Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python csv write only certain fieldnames, not all

Tags:

python

csv

I must be missing something, but I don't get it. I have a csv, it has 1200 fields. I'm only interested in 30. How do you get that to work? I can read/write the whole shebang, which is ok, but i'd really like to just write out the 30. I have a list of the fieldnames and I'm kinda hacking the header.

How would I translate below to use DictWriter/Reader?

for file in glob.glob( os.path.join(raw_path, 'P12*.csv') ):
    fileReader = csv.reader(open(file, 'rb'))
    fileLength = len(file)
    fileGeom = file[fileLength-7:fileLength-4]
    table = TableValues[fileGeom]
    filename = file.split(os.sep)[-1]
    with open(out_path + filename, "w") as fileout:
        for line in fileReader:
            writer = csv.writer(fileout, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
            if 'ID' in line:
                outline = line.insert(0,"geometryTable")
            else:
                outline = line.insert(0,table) #"%s,%s\n" % (line, table)
            writer.writerow(line)
like image 912
greenwar Avatar asked Mar 29 '12 00:03

greenwar


1 Answers

Here's an example of using DictWriter to write out only fields you care about. I'll leave the porting work to you:

import csv

headers = ['a','b','d','g']

with open('in.csv','rb') as _in, open('out.csv','wb') as out:
    reader = csv.DictReader(_in)
    writer = csv.DictWriter(out,headers,extrasaction='ignore')
    writer.writeheader()
    for line in reader:
        writer.writerow(line)

in.csv

a,b,c,d,e,f,g,h
1,2,3,4,5,6,7,8
2,3,4,5,6,7,8,9

Result (out.csv)

a,b,d,g
1,2,4,7
2,3,5,8
like image 118
Mark Tolonen Avatar answered Oct 21 '22 01:10

Mark Tolonen