Làm cách nào để thêm giá trị vào các hàng từ tệp csv trong python?

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)
7 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)
    8) 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)
    9
  • 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)
    7
  • 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 để 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)
9

Phần sau đây minh họa cách ghi 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)

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

________số 8

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

Làm cách nào để thêm giá trị vào các hàng từ tệp csv trong python?
Làm cách nào để thêm giá trị vào các hàng từ tệp csv trong python?

Để bỏ dòng trống các bạn truyề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)
9 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)
1

đầu ra

Làm cách nào để thêm giá trị vào các hàng từ tệp csv trong python?
Làm cách nào để thêm giá trị vào các hàng từ tệp csv trong python?

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

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