0

I am trying to write a json dump of a list of objects to file. The object looks like this:

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 is a dictionary containing a language id and a corresponding name, some of them in Japanese, Korean and Chinese. Later on in my code, I save the list to JSON like this:

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

But instead of "フシギダネ", "이상해씨", and "妙蛙種子", I get unreadable gibberish, as shown below. Screenshot of JSON file content

How can I get Python to properly save those strings to file?

Community
  • 1
  • 1
Peter W.
  • 2,323
  • 4
  • 22
  • 42

0 Answers0