Python ghi bảng vào tệp văn bản

Theo mặc định, 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]
8 sử dụng kết quả của 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]
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 nhau

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

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 và hành vi mặc định của nó trong các bản phát hành trước

Mô tả các trường hoặc hàng nhập liệu

Mặc định R2020a

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 Hành vi

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

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 làm tên của các biến bảng đầu ra

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

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 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]
4

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]
5 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ế độ

    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] bằng cách sử dụng chức nă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]
    7
  • 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]
    5
  • 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 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

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

________số 8

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 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 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

Để 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 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 như sau

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

đầ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

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 # 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

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]
5 để ghi từ điển vào tệp CSV

Chủ Đề