Python 如何循环读取csv或者excel中的一列数据,写入到百度中搜索
发布网友
发布时间:2022-04-22 20:46
我来回答
共1个回答
热心网友
时间:2023-07-16 06:52
实测是可以从 a.csv复制到 b.csv中
import csv
def foo():
with open('a.csv', 'r') as f:
reader = csv.DictReader(f)
rows = [row for row in reader]
if not rows:
return
with open('b.csv', mode='w', newline='', errors='ignore') as f2:
for index, row in enumerate(rows):
if index == 0:
f_csv = csv.DictWriter(f2, fieldnames=list(row.keys()))
f_csv.writeheader()
f_csv.writerow(row)
if __name__ == '__main__':
foo()