Hướng dẫn create csv file python - tạo tệp csv python

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.

Nội phân chính

  • Các bước để viết tệp CSV
  • Viết vào ví dụ về tệp CSV
  • Viết nhiều hàng vào các tệp CSV
  • Ghi vào các tệp CSV bằng lớp DictWriter
  • Làm cách nào để tạo tệp CSV?
  • Tệp CSV trong Python là gì?
  • Làm cách nào để tạo tệp CSV trong gấu trúc?
  • Làm cách nào để tạo tệp CSV trong Notebook Jupyter?

Các bước để viết tệp CSV

Viết vào ví dụ về tệp CSV

  • Viết nhiều hàng vào các tệp CSV
  • Ghi vào các tệp CSV bằng lớp DictWriter
  • Làm cách nào để tạo tệp CSV?
  • Tệp CSV trong Python là gì?

Làm cách nào để tạo tệp CSV trong gấu trúc?

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]

Làm cách nào để tạo tệp CSV trong Notebook Jupyter?

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]

Để 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 open[].

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 CSV

Viết nhiều hàng vào các 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]

Ghi vào các tệp CSV bằng lớp DictWriter

Làm cách nào để tạ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', 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 vào các tệp CSV bằng lớp DictWriter

Làm cách nào để tạo tệp CSV?

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 DictWriter

Làm cách nào để tạo tệp CSV?

Tệp CSV trong Python là gì?

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 cách nào để tạo tệp CSV trong gấu trúc?

  • Làm cách nào để tạo tệp CSV trong Notebook Jupyter?
  • Để 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 open[].
  • Thứ hai, tạo đối tượng người viết 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 csv.
  • 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 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:

Nó 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:

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 open[].a type of plain text file that uses specific structuring to arrange tabular data. Because it's a plain text file, it can contain only actual text data—in other words, printable ASCII or Unicode characters. The structure of a CSV file is given away by its name.

Sau đây minh họa cách viết các ký tự UTF-8 vào tệp CSV: DataFrame. to_csv[] method you can write/save/export a pandas DataFrame to CSV File. By default to_csv[] method export DataFrame to a CSV file with comma delimiter and row index as the first column.

Bài Viết Liên Quan

Chủ Đề