Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write CSV columns out in a different order in Python

Tags:

python

csv

I realize this is very similar to this question. However, I have a CSV file that always comes in the same format that I need to write out with columns in a different order to move it down the data processing pipeline. If my csv file contains headers and data like this:

Date,Individual,Plate,Sample,test,QC
03312011,Indiv098,P342,A1,deep,passed
03312011,Indiv113,P352,C3,deep,passed

How would I write out a csv file with the same columns as the original input csv but in the following order:

test,QC,Plate,Sample
deep,passed,P342,A1
deep,passed,P352,C3

My initial thought was to do something like this:

f = open('test.csv')
lines = f.readlines()
for l in lines:
    h = l.split(",")
    a, b, c, d, e, f  = h
    for line in h:
        print e, f, c, d, 
like image 500
Stedy Avatar asked May 24 '11 23:05

Stedy


People also ask

Does CSV column order matter?

Answer: No, the order of the columns in the . csv file does not matter. There is, however, one data import type which does require some of the columns be in a specific order.

How does CSV separate columns?

CSV is a delimited data format that has fields/columns separated by the comma character and records/rows terminated by newlines.


2 Answers

If there's the slightest chance that the input file or the output file won't have the same layout each time, here's a more general way to get your "reorderfunc":

writenames = "test,QC,Plate,Sample".split(",") # example
reader = csv.reader(input_file_handle)
writer = csv.writer(output_file_handle)
# don't forget to open both files in binary mode (2.x)
# or with `newline=''` (3.x)
readnames = reader.next()
name2index = dict((name, index) for index, name in enumerate(readnames))
writeindices = [name2index[name] for name in writenames]
reorderfunc = operator.itemgetter(*writeindices)
writer.writerow(writenames)
for row in reader:
    writer.writerow(reorderfunc(row))
like image 121
John Machin Avatar answered Nov 15 '22 16:11

John Machin


reorderfunc = operator.itemgetter(4, 5, 2, 3)

 ...

newrow = reorderfunc(oldrow)
 ...
like image 43
Ignacio Vazquez-Abrams Avatar answered Nov 15 '22 17:11

Ignacio Vazquez-Abrams