Search code examples
pythoncsvconcatenationexport-to-csv

Combining multiple csv files into one csv file


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

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.

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.

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)

Solution

  • Your code works for me.

    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")