You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中json.load()读取JSON时Unicode乱码,如何保留原字符串?

解决JSON读取希腊字符乱码的问题

问题根源是读取JSON文件时未指定正确编码格式。默认情况下open()会使用系统默认编码(比如Windows下的GBK)读取文件,但你的testdata.json是UTF-8编码,导致非ASCII字符解码错误,出现乱码。

解决方案

修改文件读取代码,打开文件时显式指定编码为utf-8:

with open(file_location, encoding='utf-8') as test_data_obj: 
    pytest.data = json.load(test_data_obj)

修改后执行print(pytest.data["country"]["country_name"]),就能正确输出Ελλάδα。

内容的提问来源于stack exchange,提问作者Sapthagiri Manivannan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 12:35:29