Search code examples
pythonpython-3.xunicodepython-3.8python-unicode

open('*.json') gives "UnicodeDecodeError"


I'm trying to import a .json file as a dictionary using the following code:

import json

with open('StreamingHistory0.json') as json_file: 
    history = json.load(json_file)

However, I keep getting the following error:

File "C:\Users\User\AppData\Local\Programs\Python\Python38\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 97181: character maps to <undefined>

What is the cause of this error and is there any suitable fix for it?


Solution

  • Most probably an encoding error. Try

    with open('StreamingHistory0.json', encoding='utf-8')