将多个csv文件合并为一个csv文件 [英] Combining multiple csv files into one csv file

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

问题描述

我正在尝试将多个csv文件合并为一个,并尝试了多种方法,但是我很挣扎.

I am trying to combine multiple csv files into one, and have tried a number of methods but I am struggling.

我从多个csv文件导入数据,当我将它们编译成一个csv文件时,前几行似乎很好地填满了,但是随后它开始在行之间随机输入变量号的空格,而且它永远不会完成对合并的csv文件的填充,似乎只是不断地向其中添加信息,这对我来说没有意义,因为我正在尝试编译有限数量的数据.

I import the data from multiple csv files, and when I compile them together into one csv file, it seems that the first few rows get filled out nicely, but then it starts randomly inputting spaces of variable number in between the rows, and it never finishes filling out the combined csv file, it just seems to continuously get information added to it, which does not make sense to me because I am trying to compile a finite amount of data.

我已经尝试过为该文件编写close语句,但是我仍然得到相同的结果,我指定的组合式csv文件永远不会停止获取数据,并且它将在整个文件中随机分配数据-我只需要一个正常编译的csv .

I have already tried writing close statements for the file, and I still get the same result, my designated combined csv file never stops getting data, and it will randomly space the data throughout the file - I just want a normally compiled csv.

我的代码中有错误吗?关于为什么我的csv文件具有这种方式,是否有任何解释?

Is there an error in my code? Is there any explanation as to why my csv file is behaving this way?

csv_file_list = glob.glob(Dir + '/*.csv') #returns the file list
print (csv_file_list)
with open(Avg_Dir + '.csv','w') as f:
    wf = csv.writer(f, delimiter = ',')
    print (f)
    for files in csv_file_list:
        rd = csv.reader(open(files,'r'),delimiter = ',')
        for row in rd:
            print (row)
            wf.writerow(row)

推荐答案

您的代码对我有用.

或者,您可以按以下方式合并文件:

Alternatively, you can merge files as follows:

csv_file_list = glob.glob(Dir + '/*.csv')
with open(Avg_Dir + '.csv','w') as wf:
    for file in csv_file_list:
        with open(file) as rf:
            for line in rf:
                if line.strip(): # if line is not empty
                    if not line.endswith("\n"):
                        line+="\n"
                    wf.write(line)

或者,如果文件不是太大,则可以一次读取每个文件.但是在这种情况下,所有空行和标题都将被复制:

Or, if the files are not too large, you can read each file at once. But in this case all empty lines an headers will be copied:

csv_file_list = glob.glob(Dir + '/*.csv')
with open(Avg_Dir + '.csv','w') as wf:
    for file in csv_file_list:
        with open(file) as rf:
            wf.write(rf.read().strip()+"\n")

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

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