Theo mặc định, hàm
8 sử dụng kết quả của 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]
9 để nhập dữ liệu dạng bảng. Về bản chất, hai lệnh gọi hàm này hoạt động giống hệt nhauCode 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 = readtable[filename] T = readtable[filename,detectImportOptions[filename]]
Trong R2020a, có một số khác biệt giữa hành vi mặc định của
8 và hành vi mặc định của nó trong các bản phát hành trướ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]
Mô tả các trường hoặc hàng nhập liệu
Mặc định R2020a
8 Hành viCode 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]
Hành vi mặc định trong các bản phát hành trước
Hàng đầu tiên không có văn bản để gán làm tên của các biến bảng đầu ra
Gán tên
2 làm tên của các biến bảng đầu raCode 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]
Chuyển đổi các giá trị trong hàng đầu tiên của giá trị dữ liệu thành tên của các biến bảng đầu ra
Nhiều hàng văn bản dưới dạng dòng tiêu đề
Bỏ qua các dòng tiêu đề bổ sung
Nhập các giá trị trong các hàng còn lại dưới dạng các loại dữ liệu được phát hiện
Nhập các dòng tiêu đề bổ sung dưới dạng văn bản trong các hàng đầu tiên của bảng đầu ra
Nhập các giá trị trong các hàng còn lại dưới dạng văn bản
trường trống
Coi như các giá trị bị thiếu đối với loại dữ liệu được phát hiện
Coi như các vectơ hoặc chuỗi ký tự trống
Giá trị trong dấu ngoặc kép
Coi như loại dữ liệu được phát hiện
Coi như văn bản
Văn bản không thể chuyển đổi
Coi như các giá trị bị thiếu đối với loại dữ liệu được phát hiện
Coi như văn bản
Ký tự không phải số nối tiếp ký tự số mà không có dấu phân cách giữa chúng
Coi các ký tự là không phải số
Xử lý các ký tự số và không phải số như thể dấu phân tách chúng
Tệp văn bản đầu vào có các dòng có số lượng dấu phân cách khác nhau
Trả về bảng đầu ra với các biến phụ
Tăng thông báo lỗi
Để gọi
8 với hành vi mặc định mà nó có cho đến R2019b, hãy sử dụng đối số cặp tên-giá trị
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]
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]
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
5 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ế độ
6] bằng cách sử dụng chức nă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]
7
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ứ 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]
5
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
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]
Phần sau đây minh họa cách ghi các ký tự UTF-8 vào tệp CSV
________số 8Ghi 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
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ở
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]
7 như sauCode 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]
đầ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]
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]
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]
5 để 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]