UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to

前端 未结 9 1541
情歌与酒
情歌与酒 2020-11-22 00:43

I\'m trying to get a Python 3 program to do some manipulations with a text file filled with information. However, when trying to read the file I get the following error:

9条回答
  •  囚心锁ツ
    2020-11-22 00:55

    TLDR? Try: file = open(filename, encoding='cp437)

    Why? When one use:

    file = open(filename)
    text = file.read()
    

    Python assumes the file uses the same codepage as current environment (cp1252 in case of the opening post) and tries to decode it to its own default UTF-8. If the file contains characters of values not defined in this codepage (like 0x90) we get UnicodeDecodeError. Sometimes we don't know the encoding of the file, sometimes the file's encoding may be unhandled by Python (like e.g. cp790), sometimes the file can contain mixed encodings.

    If such characters are unneeded, one may decide to replace them by question marks, with:

    file = open(filename, errors='replace')
    

    Another workaround is to use:

    file = open(filename, errors='ignore')
    

    The characters are then left intact, but other errors will be masked too.

    Quite good solution is to specify the encoding, yet not any encoding (like cp1252), but the one which has ALL characters defined (like cp437):

    file = open(filename, encoding='cp437')
    

    Codepage 437 is the original DOS encoding. All codes are defined, so there are no errors while reading the file, no errors are masked out, the characters are preserved (not quite left intact but still distinguishable).

提交回复
热议问题