python json加载设置编码为utf-8 [英] python json load set encoding to utf-8

查看:97
本文介绍了python json加载设置编码为utf-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

keys_file = open("keys.json")
keys = keys_file.read().encode('utf-8')
keys_json = json.loads(keys)
print(keys_json)

keys.json 中有一些非英文字符.但结果我得到:

There are some none-english characters in keys.json. But as a result I get:

[{'category': 'мбт', 'keys': ['Блендер Philips',
'мультиварка Polaris']}, {'category': 'КБТ', 'keys':
['холод ильник атлант', 'посудомоечная
машина Bosch']}]

我该怎么办?

推荐答案

encode 表示字符转二进制.读取一个文件时你想要的是二进制到字符decode.但实际上这整个过程太手动了,只需这样做:

encode means characters to binary. What you want when reading a file is binary to charactersdecode. 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 处理文件的正确打开和关闭,openencoding 参数确保使用正确的编码读取文件,并且 load 调用直接从文件句柄读取,而不是先在内存中存储文件内容的副本.

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.

如果这仍然输出无效字符,则意味着您的源编码不是 UTF-8,或者您的控制台/终端不处理 UTF-8.

If this still outputs invalid characters, it means your source encoding isn't UTF-8 or your console/terminal doesn't handle UTF-8.

这篇关于python json加载设置编码为utf-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆