34

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:\Users\Owner\Desktop\Exodus Logs\Log File Translater.py", line 159, in <module> main() File "C:\Users\Owner\Desktop\Exodus Logs\Log File Translater.py", line 7, in main for line in log: File "C:\Python32\lib\encodings\cp1252.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> 
8
  • What encoding is the file in? Commented May 13, 2013 at 18:14
  • Strangly, This seems to only occured if I used specific file. It stopped at spefiic line as well. Commented May 13, 2013 at 18:14
  • 3
    Your windows default encoding is cp1252 but the file is not using that encoding. Commented May 13, 2013 at 18:15
  • 3
    @Bugboy1028 By definition, you cannot find an encoding in the decoded file itself. You always have to remember it alongside the file, or devise a detection scheme for your file format. Commented May 13, 2013 at 18:17
  • 2
    you can try chardet to guess the encoding Commented May 13, 2013 at 18:20

1 Answer 1

61

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

Sign up to request clarification or add additional context in comments.

3 Comments

The encoding parameter isn't available on Python 2
In my case, I had a text file with Windows CRLF instead of Unix LF.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.