1

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!

cybertextron
  • 10,547
  • 28
  • 104
  • 208
spinodal
  • 670
  • 6
  • 15

4 Answers4

2

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

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
2

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.

jmetz
  • 12,144
  • 3
  • 30
  • 41
1

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.

gepatino
  • 160
  • 2
1

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)
Karol Nowak
  • 662
  • 3
  • 8