0

I have opened and read csv files in python many times before, but for some reason I keep getting this error this time.

Error:

File "C:\Program Files\Python37\lib\encodings\cp1250.py", line 23, in decode
    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>

My code:

with open("databa.csv", 'r') as fp:
    for line in fp:
        print(line)

I have already tried renaming the file, setting the format as 'UTF8' or ignoring the errors ("databa.csv", 'r', errors='ignore') but none of it worked

with open(r"databa.csv", 'rb') as fp: 
with open("databa.csv", 'r', buffering=0) as fp: 
with open("databa.csv", errors="ignore") as fp: 
with open("databa.csv",encoding='utf8', errors="ignore") as fp: 
with open("databa.csv",encoding='utf8', "r") as fp: 
with open("databa.csv", encoding='utf8', "r") as fp
David D.
  • 109
  • 1
  • 10
  • `I have already tried...setting the format as 'UTF-8'` can you show what you tried? – artemis Sep 24 '19 at 19:45
  • I have tried this: with open(r"databa.csv", 'rb') as fp: with open("databa.csv", 'r', buffering=0) as fp: with open("databa.csv", errors="ignore") as fp: with open("databa.csv",encoding='utf8', errors="ignore") as fp: with open("databa.csv",encoding='utf8', "r") as fp: with open("databa.csv", encoding='utf8', "r") as fp: – David D. Sep 24 '19 at 19:50
  • Can you paste some contents of the csv file? – artemis Sep 24 '19 at 19:51
  • and many other combinations but none of them worked. – David D. Sep 24 '19 at 19:51
  • 2
    edit question and put code from comment. It will be more readable in question. – furas Sep 24 '19 at 19:52
  • Rank Rating Title No. of Reviews 1 99% The Wizard of Oz (1939) 109 2 100% The Third Man (1949) 76 3 100% Citizen Kane (1941) 73 4 97% Mad Max: Fury Road (2015) 357 – David D. Sep 24 '19 at 19:52
  • 1
    always put code, data and error messages in question. it will be more readable. You can't format it in comment. – furas Sep 24 '19 at 19:53
  • this file can be in different encoding than `utf-8` or `cp1250` (which you see in error message). When I try `b'\x90'.decode('utf-8')` then I get error. The same with `decode('cp1250')` but it runs without error for `decode('latin1')` (popular in Windows) – furas Sep 24 '19 at 19:56
  • You need check what is the file encoding https://stackoverflow.com/questions/9233027/unicodedecodeerror-charmap-codec-cant-decode-byte-x-in-position-y-character/9233174#9233174 – Joao Vitorino Sep 24 '19 at 20:18

1 Answers1

1

I ran the example you provided and it works perfectly fine without any issues:

enter image description here

Sample data provided:

enter image description here

Output:

enter image description here

algorythms
  • 1,547
  • 1
  • 15
  • 28