2

I have a csv file and it contains 4 blank records (rows[0]) in csv file at row 147. and I have to ignore them and continue reading other rows in csv file.

For the blew logic it captures only records till the point it hits blank row and exits i.e row(146) and rest of the rows are ignored.

counter=0
with open('file_march15_l1.CSV','rU') as infile:
    reader=csv.reader( (line.replace('\0','') for line in infile) )          
    for row in reader:
        try:
            if  row[0]:
                counter=counter+1
                print('==='+row[1]+'===>')
                result[row[0]].append(row[1])
        except IndexError:
            pass
    pp.pprint(len(result))
    print("Count rows:"+str(counter))

output of above snippet is:
146
Count rows:1049
==
So here I need to capture all records except 4 blank records.

I tired looking at below link and no luck as it capture till row 146 and rest of the rows are ignored(i.e after blank records). Python CSV error: line contains NULL byte

Appreciate any help to capture all records!

Community
  • 1
  • 1
Vinod HC
  • 1,557
  • 5
  • 20
  • 38
  • Have you tried using Numpy? http://stackoverflow.com/questions/3518778/how-to-read-csv-into-record-array-in-numpy – twothreebrent Mar 15 '16 at 14:27
  • It is hard to tell without looking at the file but can you simply do ``if row:`` ? This is how I deal with empty lines. You don't need to use an index or try/except block. – Igor Mar 15 '16 at 14:35
  • [This answer](http://stackoverflow.com/a/30586957/1843248) should resolve your question. – Deacon Mar 15 '16 at 14:39
  • The file format id 'data' and not "ASCII text" this might be the reason I am facing issue. ex: >> file file_march15_l1.CSV >> file_march15_l1.CSV: data – Vinod HC Mar 15 '16 at 14:48

0 Answers0