I have the following CSV file

The data is not stored in an ideal format. The first row has some pretty useless and repeated tags.

What I am trying to do is to get something similar to the following into a file.

P1000, N400
P1001, N401

Two challenges I am having:

#1. I want to skip the first row
#2. I would like to skip fields 0 and 6 so that if a value is not blank then it will write a line with the PCID and the value.

I have the following code:

import csv, sys
filename = "test.csv"
reader = csv.reader(open(filename, "rb"))
    for row in reader:
            PCID = row[6]
            for field in row:
                if field != "":
                    print PCID,field
except csv.Error, e:
    sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))

You could replace the for field in row part by this

fields = [f for f in row[1:6] if f]
if fields:
    print "%s,%s" %(PCID, fields[0])
    raise Exception("No field for %s" % PCID)
for row in rows[1:]:
    print filter(len, row[1:6])

rows[1:] skips row 0. The filter looks at items 1 to 5 and filters out items with 0 length. It does the same thing as [f for f in row[1:6] if f] but it's much nicer to read...


This article has been dead for over six months. Start a new discussion instead.