JSON值被转换为unicode-python



我当前的Python Appium测试自动化框架使用JSON.load((从JSON文件读取数据

";λάδα"被转换为"strong>";岛当调用json.load((方法时。

很高兴为我指出一个解决方案,即我可以保持实际的字符串值";λάδα">

testdata.JSON中的JSON数据

{
"country" : {"country_name" : "Ελλάδα"}
}

.py文件中的JSON加载

with open(file_location) as test_data_obj: 
pytest.data = json.load(test_data_obj)

这打印出";岛

print(pytest.data["country"]["country_name"])

使用with open(file_location,encoding='utf-8')。无论出于何种原因,您的系统都使用拉丁文1作为默认编码,而不是UTF8。

文件很好。这个页面,像99.9%的网页一样是UTF8。这就是为什么你能够毫无问题地写出¦λλάδα。

Python 3字符串也是Unicode。如果您在Python控制台中键入"Ελλάδα",您将返回字符串。

为了得到你发布的内容,我必须使用latin-1:解码字节

>>> "Ελλάδα".encode('utf-8').decode('latin-1')
'Îx95λλάδα'

最新更新