热线电话:13121318867

登录
2019-01-18 阅读量: 848
如何将多个csv文件整理成一个csv文件?

问题描述:

想要按时间顺序将所有这些csv文件放入一个csv文件中,但是在每次放入新的csv文件时都要手动删除标题,有什么方法可以不用删除标题,快速将多个csv文件整理成一个csv文件?

csv文件一

问题解决

按时间对CSV文件进行排序(可能这可以使用字母数字排序的文件名来完成),然后将它们连接在一起。在python中可以这样实现:

from glob import glob
# Fetch a sorted list of all .csv files
files = sorted(glob(
'*.csv'))

# Open output file for writing
with open('cat.csv', 'w') as fi_out:
# iterate over all csv files
for i, fname_in in enumerate(files):
# open each csv file
with open(fname_in, 'r') as fi_in:
# iterate through all files in the csv file
for i_line, line in enumerate(fi_in):
# Write all lines of the first file (i == 0)
# For all other files write all lines except the first one (i_line > 0)
if i_line > 0 or i == 0:
fi_out.write(line)
6.0509
1
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子