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, 
5
  • If you just need a tool rather than code to do this, take a look at my FOSS tool at code.google.com/p/csvfix which makes re-ordering of CSV fields (among other things) trivial. Commented May 24, 2011 at 23:05
  • 3
    Famous Last Words: "file that always comes in the same format" Commented May 24, 2011 at 23:08
  • docs.python.org/library/csv.html Commented May 24, 2011 at 23:14
  • Famous Last Thoughts: "My CSV file can be parsed successfully with str.split(',')" Commented May 24, 2011 at 23:35
  • new_row = [row[4], row[5], row[2], row[3]] See stackoverflow.com/questions/8905165/… Commented Dec 22, 2015 at 21:36

4 Answers 4

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))
Sign up to request clarification or add additional context in comments.

Comments

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

 ...

newrow = reorderfunc(oldrow)
 ...

Comments

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

1 Comment

the best python working python code, who cares for speed nowadays thanks to moore laws
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])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.