Python Newbie hơi thất vọng với mô -đun CSV. Với tốc độ này, sẽ dễ dàng hơn nếu tôi tự viết trình phân tích cú pháp tập tin, nhưng tôi muốn làm mọi thứ theo cách Pythonic ....
Tôi đã viết một tập lệnh Python nhỏ sẽ lưu dữ liệu của tôi vào tệp CSV.
Đây là một đoạn mã của tôi:
import csv
wrtr = csv.writer[open['myfile.csv','wb'],delimiter=',', quotechar='"']
for row in rows:
wrtr.writerow[[row.field1,row.field2,row.field3]]
Tệp myfile.csv được tạo thành công, nhưng nó trống - nhưng có khóa trên đó, vì nó vẫn đang được sử dụng bởi quy trình Python. Có vẻ như dữ liệu đã được ghi vào tệp trong bộ nhớ, nhưng nó vẫn chưa được chuyển vào đĩa.
Vì quy trình Python đang giữ một khóa trên tệp, thì tôi cho rằng tôi chịu trách nhiệm phát hành khóa. Đây là những câu hỏi của tôi:
- Làm thế nào để tôi có được Python để xả vào đĩa
- Làm cách nào để đóng tệp được mở trong phương thức csv.Writer []?
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 csv
tích hợp.: in this tutorial, you’ll learn how to write data into a CSV file using the built-in csv
module.
Các bước để viết tệp CSV
Để ghi dữ liệu vào tệp CSV, bạn làm theo các bước sau:
- Đầu tiên, hãy mở tệp CSV để viết [chế độ
w
] bằng cách sử dụng hàm
0.
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 người viết CSV bằng cách gọi hàm
1 của mô -đun
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[]
csv
. - Thứ ba, ghi dữ liệu vào tệp CSV bằng cách gọi phương thức
3 hoặc
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[]
4 của đối tượng người viết CSV.
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[]
- Cuối cùng, đóng tệp sau khi bạn hoàn thành việc viết dữ liệu cho nó.
Mã sau đây 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[]
Nó sẽ ngắn hơn nếu bạn sử dụng câu lệnh
5 để bạn không cần gọi phương thứcCode 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[]
6 để đóng tệp một cách rõ ràng: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[]
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 bạn 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 ____10.
Sau đây minh họa cách viết 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]
Viết vào ví dụ về tệp CSV
Ví dụ sau đây cho thấy cách ghi dữ liệu vào tệp CSV:
Code language: PHP [php]
import csv header = ['name', 'area', 'country_code2', 'country_code3'] data = ['Afghanistan', 652090, 'AF', 'AFG'] with open['countries.csv', 'w', encoding='UTF8'] as f: writer = csv.writer[f] # write the header writer.writerow[header] # write the data writer.writerow[data]
Nếu bạn mở
8, bạn sẽ thấy một vấn đề rằng nội dung tệp có thêm dòng trống giữa hai hàng tiếp theo: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[]
Để xóa dòng trống, bạn chuyển đối số từ khóa
9 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]
0 như sau: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[]
Code language: PHP [php]
import csv header = ['name', 'area', 'country_code2', 'country_code3'] data = ['Afghanistan', 652090, 'AF', 'AFG'] with open['countries.csv', 'w', encoding='UTF8', newline=''] as f: writer = csv.writer[f] # write the header writer.writerow[header] # write the data writer.writerow[data]
Output:
Viết nhiều hàng vào các 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
4 của đối tượng người viết CSV.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[]
Sau đây sử dụng phương thức
4 để ghi nhiều hàng vào tệpCode 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[]
8: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[]
Code language: PHP [php]
import csv header = ['name', 'area', 'country_code2', 'country_code3'] data = [ ['Albania', 28748, 'AL', 'ALB'], ['Algeria', 2381741, 'DZ', 'DZA'], ['American Samoa', 199, 'AS', 'ASM'], ['Andorra', 468, 'AD', 'AND'], ['Angola', 1246700, 'AO', 'AGO'] ] with open['countries.csv', 'w', encoding='UTF8', newline=''] as f: writer = csv.writer[f] # write the header writer.writerow[header] # write multiple rows writer.writerows[data]
Ghi vào các tệp CSV bằng lớp DictWriter
Nếu mỗi hàng của tệp CSV là từ điển, bạn có thể sử dụng lớp
4 của mô -đunCode 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]
csv
để viết từ điển vào tệp CSV.Ví dụ minh họa cách sử dụng lớp DictWriter để ghi dữ liệu vào tệp CSV:
Code language: PHP [php]
import csv # csv header fieldnames = ['name', 'area', 'country_code2', 'country_code3'] # csv data rows = [ {'name': 'Albania', 'area': 28748, 'country_code2': 'AL', 'country_code3': 'ALB'}, {'name': 'Algeria', 'area': 2381741, 'country_code2': 'DZ', 'country_code3': 'DZA'}, {'name': 'American Samoa', 'area': 199, 'country_code2': 'AS', 'country_code3': 'ASM'} ] with open['countries.csv', 'w', encoding='UTF8', newline=''] as f: writer = csv.DictWriter[f, fieldnames=fieldnames] writer.writeheader[] writer.writerows[rows]
Làm thế nào nó hoạt động.
- Đầu tiên, xác định các biến giữ tên trường và hàng dữ liệu của tệp CSV.
- Tiếp theo, mở tệp CSV để viết bằng cách gọi hàm
0.
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[]
- Sau đó, tạo một thể hiện mới của lớp
4 bằng cách chuyển đối tượng tệp [
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]
8] và đối số
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]
9 cho nó.
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]
- Sau đó, hãy viết tiêu đề cho tệp CSV bằng cách gọi phương thức
0.
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]
- Cuối cùng, ghi các hàng dữ liệu vào tệp CSV bằng phương thức
4.
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[]
Bản tóm tắt
- Sử dụng người viết CSV hoặc lớp
4 để ghi dữ liệu 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'] as f: # create the csv writer writer = csv.writer[f] # write a row to the csv file writer.writerow[row]
Bạn có thấy hướng dẫn này hữu ích không?