8

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, 
Community
  • 1
  • 1
Stedy
  • 7,359
  • 14
  • 57
  • 77

4 Answers4

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

 ...

newrow = reorderfunc(oldrow)
 ...
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
5

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))
John Machin
  • 81,303
  • 11
  • 141
  • 189
4

Given your input as src.csv:

import csv

with open('x.csv','rb') as i:
    with open('y.csv','wb') as o:
        r = csv.DictReader(i)
        w = csv.DictWriter(o,'test QC Plate Sample'.split(),extrasaction='ignore')
        w.writeheader()
        for a in r:
            w.writerow(a)

Output

test,QC,Plate,Sample
deep,passed,P342,A1
deep,passed,P352,C3
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
0
#Use CSV library
import csv
media = {}
files=['Online.txt']
directory = "C:/directory/"
rowCnt=0

for file in files:

    file=directory+file

    with open(file, 'rb') as f:
        reader = csv.reader(f, delimiter='|') #use pipe delimiter

        for row in reader:
            rowCnt+=1
            if (rowCnt % 1000) == 0:
                print ('"%s","%s","%s","%s","%s","%s","%s","%s","%s"')% (row[1],row[4],row[14],row[17],row[18],row[24],row[25],row[28],row[30])