Select to view content in your preferred language

csv. reader: line contains NULL byte

33866
1
01-08-2015 10:05 PM
SuiTao
by
New Contributor II

Hi everyone:

I wrote the following code to transform the data in a csv file and write to another csv file:

import csv
import re
fp
= r'C:\data\input.csv'
fpw
= r'C:\data\output.csv'

with open(fp, 'rb') as input, open(fpw, 'wb') as output:
     for row in csv.reader(input😞

          try:

              stop = row[11]  # find a particular field
              extr
= re.search(r"\[([A-Za-z0-9_]+)\]", stop)  # extract value enclosed by brackets in that field
              stop_id
= str(extr.group(1))
              row
[11] = stop_id  # replace the original field with the extracted value
              repl_row
= ','.join(row) + '\n'
              output
.write(repl_row)  # write the tweaked row to another csv file
        
except csv.Error:
             
pass

The code worked half way through the file and stopped due to an error raised, that said 'for row in csv.reader, line contains NULL byte'. I want to skip this error and proceed. Despite the except statement, it still stopped. Does anyone know how to solve this issue?

Much appreciate, Sui

0 Kudos
1 Reply
KishorGhatage
Occasional Contributor III

Try using following modification:

with open(fp, 'rU') as input, open(fpw, 'wb') as output:

     for row in csv.reader((line.replace('\0','') for line in input), delimiter=","):

0 Kudos