[Tutor] UnicodeDecodeError while parsing a .csv file.
SM
sunithanc at gmail.com
Mon Oct 28 23:13:59 CET 2013
Hello,
I have an extremely simple piece of code which reads a .csv file, which has
1000 lines of fixed fields, one line at a time, and tries to print some
values.
1 #!/usr/bin/python3
2 #
3 import sys, time, re, os
4
5 if __name__=="__main__":
6
7 ifd = open("infile.csv", 'r')
8
9 linenum = 0
10 for line in ifd:
11 line1 = re.split(",", line)
12 total = 0
13 if linenum == 0:
14 linenum = linenum + 1
15 continue
16 #if linenum == 126:
17 #linenum = linenum + 1
18 #break
19 print("LINE: ", linenum, line1[1])
20 for i in range(1,8):
21 if line1[i].strip():
22 print("line[i] ", int(line1[i]))
23 total = total + int(line1[i])
24 print("Total: ", total)
25
26 if total >= 4:
27 print("POSITIVE")
28 else:
29 print("Negative")
30 linenum = linenum + 1
31 ifd.close
It works fine till it parses the 1st 126 lines in the input file. For the
127th line (irrespective of the contents of the actual line), it prints the
following error:
Traceback (most recent call last):
File "p1.py", line 10, in <module>
for line in ifd:
File "/usr/lib/python3.2/codecs.py", line 300, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 1173:
invalid continuation byte
$
I am not able to figure out the cause of this error. Any clues as to why I
am seeing this error, are appreciated.
Thanks,
-SM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131028/cbcef6e7/attachment.html>
More information about the Tutor
mailing list