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
0Code language: PHP [php]
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]
1Code language: PHP [php]
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]
2Code language: PHP [php]
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]
3Code language: PHP [php]
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]
4Code language: PHP [php]
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]
5Code language: PHP [php]
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]
6Code language: PHP [php]
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]
7Code language: PHP [php]
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]
8Code language: PHP [php]
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]
7Code language: PHP [php]
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]
01Code language: PHP [php]
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]
02Code language: PHP [php]
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]
03Code language: PHP [php]
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]
04Code language: PHP [php]
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]
4Code language: PHP [php]
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]
06Code language: PHP [php]
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]
07Code language: PHP [php]
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]
08Code language: PHP [php]
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]
09Code language: PHP [php]
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]
054Code language: PHP [php]
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]
11Code language: PHP [php]
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]
4Code language: PHP [php]
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]
13Code language: PHP [php]
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]
Để 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ố
3 để ngăn các dòng trống
Code language: PHP [php]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]
- 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
4
Code language: PHP [php]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]
- Viết một bộ dữ liệu
5 tại một thời điểm bằng cách chuyển nó vào phương thức
Code language: PHP [php]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]
6
Code language: PHP [php]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]
Đâ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
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
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à
7 ngay trước khi bắt đầu lặp lại từ điển
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]
🌍 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. ]
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
1 tích hợpCode language: PHP [php]
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]
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
2
Code language: Python [python]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[]
- Thứ hai, tạo đối tượng trình ghi CSV bằng cách gọi hàm
0 của mô-đun
Code language: PHP [php]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]
1
Code language: PHP [php]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]
- Thứ ba, ghi dữ liệu vào tệp CSV bằng cách gọi phương thức
2 hoặc
Code language: PHP [php]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]
3 của đối tượng trình ghi CSV
Code language: PHP [php]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]
- 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
Code language: Python [python]
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[]
Sẽ ngắn hơn nếu bạn sử dụng câu lệnh
4 để bạn không cần gọi phương thứcCode language: PHP [php]
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]
5 để đóng tệp một cách rõ ràngCode language: PHP [php]
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]
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]
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
2Code language: Python [python]
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[]
Phần sau đây minh họa cách ghi các ký tự UTF-8 vào tệp CSV
Code language: PHP [php]
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]
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ở
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 theoCode language: PHP [php]
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]
Để loại bỏ dòng trống, bạn chuyển đối số từ khóa
8 cho hàmCode language: PHP [php]
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]
2 như sauCode language: Python [python]
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[]
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
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
3 của đối tượng trình ghi CSVCode language: PHP [php]
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]
Sau đây sử dụng phương thức
3 để ghi nhiều hàng vào tệpCode language: PHP [php]
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]
7Code language: PHP [php]
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]
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
3 của mô-đunCode language: PHP [php]
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]
1 để ghi từ điển vào tệp CSVCode language: PHP [php]
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]