Sto lavorando con alcuni file CSV, con il seguente codice:Python CSV errore: riga contiene byte NULL
reader = csv.reader(open(filepath, "rU"))
try:
for row in reader:
print 'Row read successfully!', row
except csv.Error, e:
sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
e un file sta gettando questo errore:
file my.csv, line 1: line contains NULL byte
cosa posso fare? Google sembra suggerire che potrebbe trattarsi di un file Excel che è stato salvato come .csv in modo errato. C'è un modo per aggirare questo problema in Python?
AGGIORNAMENTO == ==
seguito @ commento di JohnMachin di seguito, ho provato ad aggiungere queste righe al mio script:
print repr(open(filepath, 'rb').read(200)) # dump 1st 200 bytes of file
data = open(filepath, 'rb').read()
print data.find('\x00')
print data.count('\x00')
e questo è l'output che ho ottenuto:
'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1\x00\x00\x00\x00\x00\x00\x00\x00\ .... <snip>
8
13834
Quindi il file contiene infatti NUL byte.
Cosa significa "od -c" che assomiglia alla prima riga? –
quale query dovrei eseguire, qualcosa come cat my.csv | od -c | Di Più ? con quello ottengo: 0000000 D e p a r t m e n t F a m i l – AP257
Come viene generato il CSV? Da Excel, potresti provare un dialetto. Altrimenti guarda: http://stackoverflow.com/questions/2753022/python-finding-unicode-asci-problems –