0

我正在尝试编写要归档的对象列表的 json 转储。该对象如下所示:

class Pokemon:
    def __init__(self, id, names, genus = None):
        self.id = id
        self.names = names

    def dict(self):
        return OrderedDict([("id", self.id), ("names", self.names)]) #force specific key order

self.names是一个包含语言 id 和相应名称的字典,其中一些是日文、韩文和中文。稍后在我的代码中,我将列表保存为 JSON,如下所示:

with codecs.open("pkmn.json", "w", encoding="utf8") as outfile:
    json.dump([p.dict() for p in pokemon], outfile, indent = 2)

但我得到的不是“フシギダネ”、“이상해씨”和“妙蛙种子”,而是无法阅读的乱码,如下所示。 JSON文件内容截图

如何让 Python 正确地将这些字符串保存到文件中?

4

0 回答 0