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,
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.
CSV is a delimited data format that has fields/columns separated by the comma character and records/rows terminated by newlines.
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))
reorderfunc = operator.itemgetter(4, 5, 2, 3)
...
newrow = reorderfunc(oldrow)
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With