Cập nhật các dòng tại chỗ trong một tệp không được hỗ trợ trên hầu hết các hệ thống tệp (một dòng trong một tệp chỉ là một số dữ liệu kết thúc bằng Newline, dòng tiếp theo bắt đầu ngay sau đó). Như tôi thấy, bạn có hai tùy chọn: - Có các vòng lặp tạo dữ liệu của bạn là trình tạo, bằng cách này chúng sẽ không tiêu thụ nhiều bộ nhớ - bạn sẽ nhận được dữ liệu cho mỗi hàng "chỉ trong thời gian"
- Sử dụng cơ sở dữ liệu (sqlite?) Và cập nhật các hàng ở đó. Khi bạn hoàn thành - Xuất sang CSV
Ví dụ nhỏ cho phương pháp đầu tiên: from itertools import islice, izip, count
print list(islice(izip(count(1), count(2), count(3)), 10))
Điều này sẽ in [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
Mặc dù count tạo ra một chuỗi số vô hạn Tóm tắt: Trong hướng dẫn này, bạn sẽ tìm hiểu cách ghi dữ liệu vào tệp CSV bằng mô-đun [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
0 tích hợp.: in this tutorial, you’ll learn how to write data into a CSV file using the built-in [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
0 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ế độ
[(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
1) bằng cách sử dụng hàm [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
2. - Thứ hai, tạo đối tượng người viết CSV bằng cách gọi hàm
[(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
3 của mô -đun [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
0. - Thứ ba, ghi dữ liệu vào tệp CSV bằng cách gọi phương thức
[(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
5 hoặc [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
6 của đối tượng người viết CSV. - 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: 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)
Nó sẽ ngắn hơn nếu bạn sử dụng câu lệnh [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
7 để bạn không cần gọi phương thức [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
8 để đó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 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 [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
2.Sau đây minh họa cách viết 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)
Viết vào ví dụ về tệp CSVVí dụ sau đây cho thấy cách ghi dữ liệu vào tệp CSV: 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)
Code language: PHP (php)
Nếu bạn 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, 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:Để xóa dòng trống, bạn chuyển đối số từ khóa 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) 1 cho hàm [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
2 như sau: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) Code language: PHP (php)
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 [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
6 của đối tượng người viết CSV.Sau đây sử dụng phương thức [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
6 để ghi nhiều hàng vào tệp 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: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)
Code language: PHP (php)
Ghi vào các tệp CSV bằng lớp DictWriterNếu mỗi hàng của tệp CSV là từ điển, bạn có thể sử dụng lớp 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) 6 của mô -đun [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
0 để 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: 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)
Code language: PHP (php)
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
[(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
2. - Sau đó, tạo một thể hiện mới của lớp
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) 6 bằng cách chuyển đối tượng 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) 0) và đố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) 1 cho nó. - Sau đó, hãy viết tiêu đề cho 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. - Cuối cùng, hãy ghi các hàng dữ liệu vào tệp CSV bằng phương thức
[(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
6.
Bản tóm tắt
Bạn có thấy hướng dẫn này hữu ích không?
|