write CSV columns out in a different order in Python
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开发者_JAVA技巧:
h = l.split(",")
a, b, c, d, e, f = h
for line in h:
print e, f, c, d,
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)
...
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
#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])
精彩评论