Hướng dẫn how create csv file in python? - cách tạo tệp csv trong 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.

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

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

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 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().

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 CSV

Ví 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 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 đề rằng nội dung tệp có thêm dòng trống giữa hai hàng tiếp theo:

Hướng dẫn how create csv file in python? - cách tạo tệp csv trong python?

Để xóa 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 open() 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:

Hướng dẫn how create csv file in python? - cách tạo tệp csv trong python?

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

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.

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

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

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 open().
  • Sau đó, tạo một thể hiện mới của 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 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', 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)
    7) và đối số

    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)
    8 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', 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)
    9.
  • Cuối cùng, ghi các hàng dữ liệu vào tệp CSV bằ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.

Bản tóm tắt

  • Sử dụng người viết CSV hoặc 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 để ghi dữ liệu vào tệp CSV.

Bạn có thấy hướng dẫn này hữu ích không?

Làm cách nào để tạo một tệp CSV mới trong Python?

Python viết tệp CSV..
Đầu tiên, hãy mở tệp CSV để ghi (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 writer () 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 Writerow () hoặc Writerows () của đối tượng người viết CSV ..

Làm thế nào để bạn tạo một tệp CSV?

Microsoft Excel Sau khi mở, nhấp vào Tệp và chọn Lưu dưới dạng. Trong Lưu dưới dạng loại, chọn CSV (dấu phẩy được phân tách) hoặc CSV (dấu hiệu được phân định) (*. CSV), tùy thuộc vào phiên bản Microsoft Excel của bạn. Hàng cuối cùng bắt đầu với hai dấu phẩy vì hai trường đầu tiên của hàng đó trống trong bảng tính của chúng tôi.Once open, click File and choose Save As. Under Save as type, select CSV (Comma delimited) or CSV (Comma delimited) (*. csv), depending on your version of Microsoft Excel. The last row begins with two commas because the first two fields of that row were empty in our spreadsheet.

Tệp CSV trong ví dụ Python là gì?

Tệp CSV (tệp giá trị phân tách bằng dấu phẩy) là một loại tệp văn bản thuần túy sử dụng cấu trúc cụ thể để sắp xếp dữ liệu bảng.Bởi vì đó là một tệp văn bản đơn giản, nó chỉ có thể chứa dữ liệu văn bản thực tế nói cách khác, có thể in các ký tự ASCII hoặc Unicode.Cấu trúc của một tệp CSV được đưa ra bằng tên của nó.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.

Làm thế nào để bạn tạo và thêm dữ liệu vào tệp CSV trong Python?

Chuẩn bị một hàng mới vào tệp CSV hiện có bằng Writer..
Mở tệp CSV hiện tại của bạn ở chế độ phụ lục Tạo đối tượng tệp cho tệp này ..
Chuyển đối tượng tệp này cho CSV.Writer () và nhận một đối tượng nhà văn ..
Chuyển danh sách như một đối số vào hàm writerow () của đối tượng nhà văn.....
Đóng đối tượng tệp ..