简体   繁体   中英

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 2483: character maps to <undefined>

I am parsing a csv file and i am getting the below error

import os
import csv
from collections import defaultdict
demo_data = defaultdict(list)

if os.path.exists("infoed_daily _file.csv"):
    f = open("infoed_daily _file.csv", "rt")
    csv_reader = csv.DictReader(f)
    line_no = 0
    for line in csv_reader:
    line_no +=1
    print(line,line_no)

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 2483: character maps to 
<undefined>

Please advise.

Thanks.. -Prasanna.K

Error may means you have file in encoding different then UTF-8 which (probably in most systems) is used as default in open()

When I run

 b'\x81'.decode('Latin1') 
 b'\x81'.decode('Latin2') 
 b'\x81'.decode('iso8859') 
 b'\x81'.decode('iso8859-2') 

then it runs without error - so your file can be in some of these encodings (or similar encoding) and you have to use it

 open(..., encoding='Latin1')

or similar.

List of other encodings: codecs: standard encodings

f=open("myfile1.txt",'r')
print(f.read())

Well, for the above code I got an error as: 'charmap' codec can't decode byte 0x81 in position 637: character maps to so i tried changing the name of the file extension and it worked. Happy Coding Thanks! Vani

you can use '

with open ('filename.txt','r') as f:
      f.write(content)

The good thing is that it automatically closes the file after work is done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM