首页 > 解决方案 > 表情符号问题在python中读取json文件

问题描述

我有一个包含表情符号的字符串的 json 文件

{
   "messages": "This is a test -bla-bla test."
}

我的python代码是:

with open('config.json', 'r') as config_file:
    config = json.load(config_file)
print(config["messages"])

输出是:

这是一个测试ðŸ“'-bla-bla测试。

我该如何解决这个表情符号编码问题?

标签: pythonemoji

解决方案


您想要的是确保使用正确的编码保存/读取文件。

with open('config.json', 'r', encoding='utf-8') as config_file: 
    config = json.load(config_file)
print(config["messages"])

推荐阅读