encode
means characters to binary. What you want when reading a file is binary to characters → decode
. But really this entire process is way too manual, simply do this:
with open('keys.json', encoding='utf-8') as fh:
data = json.load(fh)
print(data)
with
handles the correct opening and closing of the file, the encoding
argument to open
ensures the file is read using the correct encoding, and the load
call reads directly from the file handle instead of storing a copy of the file contents in memory first.
If this still outputs invalid characters, it means your source encoding isn't UTF-8 or your console/terminal doesn't handle UTF-8.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…