Skip to content
Home » Unicode error handling with Python readlines()

Unicode error handling with Python readlines()

To solve Unicode error handling with Python 3’s readlines() error or UnicodeEncodeError: ‘charmap’ codec can’t decode byte 0x81 in position 7827: character maps to undefined follow below methods.

ERROR LOG

UnicodeEncodeError: ‘charmap’ codec can’t decode byte 0x81 in position 7827: character maps to undefined.

How to solve Unicode error handling with Python 3’s readlines() ?

Refer to the given methods to solve the issue.

Method 1:

Pass an appropriate value such as errors=ignore when creating your file object in Python 3 (assuming it is a subclass of io. Moreover, consider passing a more likely encoding than charmap (when in doubt, utf-8 is a decent way to start).

f = open('misc-notes.txt', encoding='utf-8', errors='ignore')

Method 2 :

The other alternative is to use the codecs. Import the codecs and use them to open a file such that you can avoid such issues. Refer the below code snippet :

import codecs
f = codecs.open('file_name','r',encoding='utf-8')
data = f.read()

Hope the above solution works.

Also read : IndentationError: unexpected unindent