如何在 Python 中合并 200 个 csv 文件 [英] how to merge 200 csv files in Python

查看:31
本文介绍了如何在 Python 中合并 200 个 csv 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我这里有 200 个从 SH (1) 到 SH (200) 的独立 csv 文件.我想将它们合并到一个 csv 文件中.我该怎么做?

Guys, I here have 200 separate csv files named from SH (1) to SH (200). I want to merge them into a single csv file. How can I do it?

推荐答案

正如 ghostdog74 所说,但这次有标题:

As ghostdog74 said, but this time with headers:

fout=open("out.csv","a")
# first file:
for line in open("sh1.csv"):
    fout.write(line)
# now the rest:    
for num in range(2,201):
    f = open("sh"+str(num)+".csv")
    f.next() # skip the header
    for line in f:
         fout.write(line)
    f.close() # not really needed
fout.close()

这篇关于如何在 Python 中合并 200 个 csv 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆