我是初学者,在csv文件中有以下数据,我想将“account_key”更改为“acct”。我该怎么做呢?
account_key,status,join_date,cancel_date,days_to_cancel,is_canceled
448,取消,2014-11-10,2015-01-14,65
448,取消,2014-11-05,2014-11-10,5,真
..。
..。
..。
发布于 2017-05-25 14:42:32
如果文件足够小,可以容纳到主内存中:
import csv
with open('path/to/file') as infile:
data = list(csv.reader(infile))
data[0][0] = 'acct'
with open('path/to/file', 'w') as fout:
outfile = csv.writer(fout)
outfile.writerows(data)如果文件太大,无法装入主内存:
with open('path/to/file') as fin, open('path/to/output', 'w') as fout:
infile = csv.reader(fin)
header = next(infile)
header[0] = 'acct'
outfile.writerow(header)
for row in infile:
outfile.writerow(row)https://stackoverflow.com/questions/44183255
复制相似问题