Python 2.7 如何在python(2.7)中更改csv文件的标题?

Python 2.7 如何在python(2.7)中更改csv文件的标题?,python-2.7,csv,Python 2.7,Csv,我是一名初学者,在csv文件中有以下数据,我想将“帐户密钥”更改为“帐户”。我该怎么做呢 帐户密钥、状态、加入日期、取消日期、取消天数、是否取消 448,取消,2014-11-102015-01-14,65,正确 448,取消,2014-11-052014-11-10,5,正确 ... ... …如果文件足够小,可以放入主内存: import csv with open('path/to/file') as infile: data = list(csv.reader(infile))

我是一名初学者,在csv文件中有以下数据,我想将“帐户密钥”更改为“帐户”。我该怎么做呢

帐户密钥、状态、加入日期、取消日期、取消天数、是否取消 448,取消,2014-11-102015-01-14,65,正确 448,取消,2014-11-052014-11-10,5,正确 ... ...

如果文件足够小,可以放入主内存:

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)
如果文件太大,无法放入主内存:

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)