1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python复制csv数据_如何在python中将数据从一个csv复制到另一个csv?

python复制csv数据_如何在python中将数据从一个csv复制到另一个csv?

时间:2020-08-15 22:27:09

相关推荐

python复制csv数据_如何在python中将数据从一个csv复制到另一个csv?

熊猫是一个很好的工具,但在这里是过度杀戮。csv模块就足够了,DictReader和DictWriter自动执行您需要的操作:

假设:输入:输出文件模板的第一行中最初包含头

要处理包含输出文件头子集的输入文件列表

输出:输出文件由输入文件中有关字段名的数据填充

可能代码:def populate(outfile, infilelist)

# First of all read output file field names:

with open(outfile) as fd:

rd = csv.DictReader(fd)

names = rd.fieldnames

# Reopen output file in append mode to populate if from the files of infilelist

with open(outfile, "a") as fdout:

wr = csvDictWriter(fdout, fieldnames = names)

# loop over the input files:

for filename in infilelist:

with open(filename) as fd:

rd = csv.DictReader(fd)

# simply copy the rows, one at a time

for row in rd:

wr.writerow(row)

该算法允许添加数据,即使输出文件包含的数据超过头行,并且可以处理大量数据,因为(除了文件缓冲区)内存中只保留一行。在

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。