import os
import csv
def read_csv_files(directory):
for root, dirs, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
print(file_path)
read_csv(file_path)
def read_csv(file_path):
with open(file_path, encoding='gb2312') as f:
reader = csv.reader(f)
# row 是字符串数组
for row in reader:
print(row)
def write_csv(file_path, header, rows):
# 打开文件进行写入
with open(file_path, 'w', newline='', encoding='gb2312') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(header)
# 写入数据
for row in rows.items():
writer.writerow(row)
Python 读写CSV文件
于 2024-06-23 10:54:28 首次发布