Làm thế nào để viết một cột trong csv trong python

Các tệp Giá trị được phân tách bằng dấu phẩy (CSV) thuộc loại tài liệu văn bản thuần túy trong đó thông tin dạng bảng được cấu trúc bằng một định dạng cụ thể. Tệp CSV là định dạng văn bản có giới hạn sử dụng dấu phẩy để phân tách các giá trị. Phương thức phổ biến nhất để ghi dữ liệu từ danh sách vào tệp CSV là phương thức writerow() của lớp writer và DictWriter. ví dụ 1. Tạo tệp CSV và ghi dữ liệu theo hàng vào tệp bằng cách sử dụng lớp trình ghi.  

Python3




import csv


data = {'A':42, 'B':41, 'C':40}

with open('my_file.csv', 'w') as f:
    for key, value in data.items():
        f.write(f'{key},{value}\n')
        
9

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
0

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
1

 

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
2

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
3

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
4

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
5

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
6

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
7

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
8

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
7

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
01

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
02

 

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
03

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
04

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
4

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
06

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
07

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
08

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
09

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
054

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
11

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
4

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
13

Để viết từ điển Python trong tệp CSV dưới dạng cột, tôi. e. , một cặp (khóa, giá trị) trên mỗi hàng, hãy sử dụng ba bước sau

  • Mở tệp ở chế độ ghi và sử dụng đối số

    import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

    Code language: PHP (php)
    3 để ngăn các dòng trống
  • Tạo một đối tượng nhà văn CSV
  • Lặp lại các cặp (khóa, giá trị) của từ điển bằng phương thức

    import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

    Code language: PHP (php)
    4
  • Viết một bộ dữ liệu

    import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

    Code language: PHP (php)
    5 tại một thời điểm bằng cách chuyển nó vào phương thức

    import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

    Code language: PHP (php)
    6

Đây là ví dụ mã

import csv


# Create a dictionary of key-value pairs
data = {'A':42, 'B':41, 'C':40}

# Open the file in writing mode (no blank lines)
with open('my_file.csv', 'w', newline='') as f:

    # Create a CSV writer object
    writer = csv.writer(f)

    # Write one key-value tuple per row
    for row in data.items():
        writer.writerow(row)

Tệp CSV đầu ra của bạn (cột chính tả) trông như thế này

Làm thế nào để viết một cột trong csv trong python

ví dụ 2. Trăn vani

Cách ngắn nhất, súc tích nhất và dễ nhất để viết từ điển dưới dạng tệp CSV bằng cách sử dụng các phím dict làm cột đầu tiên và các giá trị dict làm cột thứ hai là sử dụng xử lý tệp Python thông thường

  • Đầu tiên, mở tệp ở chế độ viết
  • Thứ hai, lặp lại tất cả các cặp (khóa, giá trị)
  • Thứ ba, viết từng cặp giá trị khóa trong một dòng bằng phương pháp viết tệp Python tiêu chuẩn

Đây là ví dụ cụ thể

import csv


data = {'A':42, 'B':41, 'C':40}

with open('my_file.csv', 'w') as f:
    for key, value in data.items():
        f.write(f'{key},{value}\n')
        

Một lần nữa, tệp CSV kết quả (cột chính tả) trông như thế này

Làm thế nào để viết một cột trong csv trong python

Nếu bạn cần một tiêu đề trong tệp CSV cột, bạn có thể thực hiện việc đó bằng một lệnh gọi bổ sung khác là

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
7 ngay trước khi bắt đầu lặp lại từ điển

🌍 Tài nguyên liên quan. 7 cách tốt nhất để chuyển đổi Dict sang CSV bằng Python


lập trình viên

❓ Câu hỏi. Tại sao các lập trình viên luôn trộn lẫn giữa Halloween và Giáng sinh?
❗ Trả lời. Vì ngày 31 tháng 10 bằng ngày 25 tháng 12.

(Nếu bạn chưa hiểu điều này, hãy đọc các bài viết của chúng tôi về các hàm tích hợp sẵn của Python. )

Làm thế nào để viết một cột trong csv trong python

Chris

Trong khi làm việc với tư cách là một nhà nghiên cứu trong các hệ thống phân tán, Dr. Christian Mayer tìm thấy tình yêu của mình với việc dạy sinh viên khoa học máy tính

Để giúp sinh viên đạt được mức độ thành công Python cao hơn, anh ấy đã thành lập trang web giáo dục lập trình Finxter. com. Ông là tác giả của cuốn sách lập trình nổi tiếng Python One-Liners (NoStarch 2020), đồng tác giả của loạt sách tự xuất bản Coffee Break Python, người đam mê khoa học máy tính, cộng tác viên tự do và chủ sở hữu của một trong 10 blog Python lớn nhất thế giới

Niềm đam mê của anh ấy là viết, đọc và mã hóa. Nhưng niềm đam mê lớn nhất của anh ấy là phục vụ các lập trình viên đầy tham vọng thông qua Finxter và giúp họ nâng cao kỹ năng của mình. Bạn có thể tham gia học viện email miễn phí của anh ấy tại đây

Bản tóm tắt. trong hướng dẫn này, bạn sẽ học cách ghi dữ liệu vào tệp CSV bằng mô-đun

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
1 tích hợp

Các bước để ghi tệp CSV

Để ghi dữ liệu vào tệp CSV, bạn làm theo các bước sau

  • Trước tiên, hãy mở tệp CSV để ghi (chế độ ____51) bằng cách sử dụng hàm

    import csv # open the file in the write mode f = open('path/to/csv_file', 'w') # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row) # close the file f.close()

    Code language: Python (python)
    2
  • Thứ hai, tạo đối tượng trình ghi CSV bằng cách gọi hàm

    import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

    Code language: PHP (php)
    0 của mô-đun

    import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

    Code language: PHP (php)
    1
  • Thứ ba, ghi dữ liệu vào tệp CSV bằng cách gọi phương thức

    import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

    Code language: PHP (php)
    2 hoặc

    import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

    Code language: PHP (php)
    3 của đối tượng trình ghi CSV
  • Cuối cùng, đóng tệp sau khi bạn hoàn thành việc ghi dữ liệu vào tệp

Đoạn mã sau minh họa các bước trên

import csv # open the file in the write mode f = open('path/to/csv_file', 'w') # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row) # close the file f.close()

Code language: Python (python)

Sẽ ngắn hơn nếu bạn sử dụng câu lệnh

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
4 để bạn không cần gọi phương thức

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
5 để đóng tệp một cách rõ ràng

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)

Nếu đang xử lý các ký tự không phải ASCII, bạn cần chỉ định mã hóa ký tự trong hàm

import csv # open the file in the write mode f = open('path/to/csv_file', 'w') # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row) # close the file f.close()

Code language: Python (python)
2

Phần sau đây minh họa cách ghi các ký tự UTF-8 vào tệp CSV

import csv # open the file in the write mode with open('path/to/csv_file', 'w', encoding='UTF8') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)

Ghi vào tệp CSV ví dụ

Ví dụ sau đây cho biết cách ghi dữ liệu vào tệp CSV

import csv


data = {'A':42, 'B':41, 'C':40}

with open('my_file.csv', 'w') as f:
    for key, value in data.items():
        f.write(f'{key},{value}\n')
        
0

Nếu bạn mở

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
7, bạn sẽ thấy một vấn đề là nội dung tệp có thêm một dòng trống giữa hai hàng tiếp theo

Làm thế nào để viết một cột trong csv trong python
Làm thế nào để viết một cột trong csv trong python

Để loại bỏ dòng trống, bạn chuyển đối số từ khóa

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
8 cho hàm

import csv # open the file in the write mode f = open('path/to/csv_file', 'w') # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row) # close the file f.close()

Code language: Python (python)
2 như sau

import csv


data = {'A':42, 'B':41, 'C':40}

with open('my_file.csv', 'w') as f:
    for key, value in data.items():
        f.write(f'{key},{value}\n')
        
3

đầu ra

Làm thế nào để viết một cột trong csv trong python
Làm thế nào để viết một cột trong csv trong python

Ghi nhiều hàng vào tệp CSV

Để ghi nhiều hàng vào tệp CSV cùng một lúc, bạn sử dụng phương thức

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
3 của đối tượng trình ghi CSV

Sau đây sử dụng phương thức

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
3 để ghi nhiều hàng vào tệp

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
7

import csv


data = {'A':42, 'B':41, 'C':40}

with open('my_file.csv', 'w') as f:
    for key, value in data.items():
        f.write(f'{key},{value}\n')
        
7

Ghi vào tệp CSV bằng lớp DictWriter

Nếu mỗi hàng của tệp CSV là một từ điển, bạn có thể sử dụng lớp

import csv # open the file in the write mode with open('path/to/csv_file', 'w', encoding='UTF8') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
3 của mô-đun

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)

Code language: PHP (php)
1 để ghi từ điển vào tệp CSV