spinodal
spinodal

Reputation: 680

python joining two csv files

I have two .csv files, headers.csv and corrected.csv. Theheaders.csv has all the headers, and the corrected.csv is just a bunch of organized data.

headers.csv:           
displacement, load, cputime, ...

corrected.csv:            
-990.478170,-0.000026,15:08:06, ...              
-990.038170,-0.000026,15:08:06, ...

The end goal is to be like this example:      
displacement,load,cputime, ...          
-990.478170,-0.000026,15:08:06, ...              
-990.038170,-0.000026,15:08:06, ...

What I have:

headers = [x for x in csv.reader(open('headers.csv', 'rb'))]
writer = csv.writer(open('merged.csv', 'wb'))
writer.writerow(headers)
for row in csv.reader(open('corrected.csv', 'rb')):
    writer.writerow(row)

The result, however, is that "['displacement', 'load', 'cputime', ...]" is all written to column A while I want displacement in column A, load in column B, cputime in column C, etc. I also want to get rid of the ', ", [], and whitespaceso the end result is exactly like my example above. Thanks in advance!

Upvotes: 1

Views: 1725

Answers (4)

Karol Nowak
Karol Nowak

Reputation: 662

I'd just hide the fact that you have multiple files from the csv module:

import csv

def cat(*files):
    for f in files:
        with open(f) as fobj:
            for line in fobj:
                yield line

writer = csv.writer(open('merged.csv', 'wb'))
for row in csv.reader(cat('headers.csv', 'corrected.csv')):
    writer.writerow(row)

Upvotes: 1

gepatino
gepatino

Reputation: 168

In the first line you are creating a list (a comprehension list) with all the lines in headers.csv, that's why you have the [], etc.

Try with this (from the top of my mind):

headers = csv.reader(open('headers.csv', 'rb'))[0]

Which should return the first row only.

Upvotes: 1

jmetz
jmetz

Reputation: 12773

Using python to concatenate the files seems overkill -

cat headers.csv corrected.csv > merged.csv

If you have to/ for some reason want to use Python, Jon Clements has the right idea.

Upvotes: 2

Jon Clements
Jon Clements

Reputation: 142136

Assuming that you have a single row with comma delimited column names, try: headers = next(csv.reader(open('headers.csv')))

Upvotes: 2

Related Questions