0

*I am having an issue processing CSV'S. I get this error:

return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 22: character maps to <undefined>

What do I need to do to fix this? I think there is an issue where the CSV matches with the user's input.*

import csv

csvanswer=input("Type your issue here!").lower() 

searchanswer=(csvanswer).split() 

userinput = open("task2csv.csv")  

csv_task=csv.reader(userinput)

for row in csv_task:
    if row[0] in searchanswer: 

        print(row) 

        break 
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • see http://stackoverflow.com/questions/9233027/unicodedecodeerror-charmap-codec-cant-decode-byte-x-in-position-y-character – jayhendren Mar 01 '16 at 21:05
  • What character set is `task2csv.csv` using? You may have to use a codecs.open on it. – mpez0 Mar 01 '16 at 21:06
  • @mpez0: For Py3 code, you don't need (and shouldn't use) `codecs.open`; the `open` built-in supports multiple encodings already (and it's usually faster than `codecs.open` IIRC). Even on Py2.6/2.7, you usually want to use [`io.open`](https://docs.python.org/2/library/io.html#io.open), not `codecs.open`; it's only on 2.5 and earlier that `codecs` is the best option (by virtue of no other good options existing). – ShadowRanger Mar 01 '16 at 21:09
  • I have seen this error when my windows concole was not able to display characters my python script was outputting. As others have mentioned, make sure your .csv file does not have "smart quotes" and such. You can check by opening it in a text edior such as notepad++ and looking for weird characters. Also, try using pprint module instead of print. I think that allowed me to print. – Igor Mar 01 '16 at 21:32
  • @ShadowRanger Thanks for the info -- I was only considering 2.x, and hadn't noted io.open. I'm using `codecs` elsewhere in my code, and just used codecs.open. – mpez0 Mar 02 '16 at 14:36

1 Answers1

1

Your input file is probably in an encoding other than the default on your system. You can fix this by explicitly providing the correct file encoding to the open call (you should also pass newline='' to the open call to properly obey the csv module's requirements).

For example, if the file is UTF-8, you'd do:

userinput = open("task2csv.csv", encoding='utf-8', newline='')

If it's some other encoding (UTF-16 is common for files produced by Windows programs), you'd use that. If it's some terrible non-UTF encoding, you're going to have to figure out the locale settings on the machine that produced it, they could be any number of different encodings.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271