While reading file on Python, I got a UnicodeDecodeError. What can I do to resolve this?

Question:

This is one of my own projects. This will later help benefit other people in a game I am playing (AssaultCube). Its purpose is to break down the log file and make it easier for users to read.

I kept getting this issue. Anyone know how to fix this? Currently, I am not planning to write/create the file. I just want this error to be fixed.

The line that triggered the error is a blank line (it stopped on line 66346).

This is what the relevant part of my script looks like:

log  =  open('/Users/Owner/Desktop/Exodus Logs/DIRTYLOGS/serverlog_20130430_00.15.21.txt', 'r')
for line in log:

and the exception is:

Traceback (most recent call last):
  File "C:UsersOwnerDesktopExodus LogsLog File Translater.py", line 159, in <module>
    main()
 File "C:UsersOwnerDesktopExodus LogsLog File Translater.py", line 7, in main
    for line in log:
  File "C:Python32libencodingscp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 3074: character maps to <undefined>
Asked By: Bugboy1028

||

Answers:

Try:

enc = 'utf-8'
log = open('/Users/Owner/Desktop/Exodus Logs/DIRTYLOGS/serverlog_20130430_00.15.21.txt', 'r', encoding=enc)

if it won’t work try:

enc = 'utf-16'
log = open('/Users/Owner/Desktop/Exodus Logs/DIRTYLOGS/serverlog_20130430_00.15.21.txt', 'r', encoding=enc)

you could also try it with

enc = 'iso-8859-15'

also try:

enc = 'cp437'

wich is very old but it also has the “ü” at 0x81 wich would fit to the string “üßer” wich I found on the homepage of assault cube.

If all the codings are wrong try to contact some of the guys developing assault cube or as mentioned in a comment: have a look at https://pypi.python.org/pypi/chardet

Answered By: Rouven B.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.