Ошибка 'utf-8' codec can't decode byte 0xd1 in position 8: invalid continuation byte

Прошу помочь как устранить ошибку. Ошибка выходит при попытке открыть json файл на jupyter notebook. Привожу код:

import json
from pprint import pprint
with open('recipes.json') as f:
   recipes = json.load(f)
pprint(recipes)

Ответы (2 шт):

Автор решения: ツPenguin

Попробуйте использовать

with open("recipes.json", encoding="UTF-8"): ...
→ Ссылка
Автор решения: Delvin

имеем файл wheremi.json:

{"status":"success","country":"USA Ñ","countryCode":"US","region":"VA","regionName":"Virgin","city":"Ashbern","zip":"","lat":39.0437,"lon":-77.4874,"timezone":"America/New_York","isp":"Google LLC","org":"Palo Alto Networks","as":"AS396982 Google LLC","query":"130.41.47.235"}

Пытаемся открыть:

import json
    with open('wheremi.json') as f:
       recipes = json.load(f)
    print(recipes['country'])

На выходе:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd1 in position 35: invalid continuation byte

Делаем:


import json
from chardet.universaldetector import UniversalDetector

detector = UniversalDetector()

file = 'wheremi.json'

for line in open(file, 'rb'):
    detector.feed(line)
    if detector.done: break
    detector.close()


with open(file, encoding=detector.result['encoding']) as f:
    recipes = json.load(f)
print(recipes['country'])

Получаем:

USA Ñ
→ Ссылка