1

I'm currently writing a modding tool and as part of that I need to be able to read in text based game data files. I am currently reading them into my program and converting them to a dictionary like string format e.g.

{"add_namespace":"ace_promoted",
"add_namespace":"ace_died",
"add_namespace":"ace_killed_by_ace",
"add_namespace":"ace_killed_other_ace",
"add_namespace":"aces_killed_each_other",
"country_event":{"id":"ace_promoted.1",
"title":"ace_promoted.1.t",
"desc":{"text":"ace_promoted.1.d",
    "trigger":{"NOT":{"tag":"GER"},
        "NOT":{"tag":"ITA"},
        "NOT":{"tag":"FRA"},
        "NOT":{"tag":"JAP"},
        "NOT":{"tag":"USA"},
        "NOT":{"tag":"ENG"},
        "NOT":{"tag":"SOV"}}}

etc...

when I use

ast.literal_eval(dict_string)

it converts the string in a dictionary but the duplicate keys mean that many values are lost as only the last is kept. Is there a way to merge these values into a list automatically so none are lost?

  • You don't have Python code, you have **JSON**. `ast.literal_eval()` is very limited (deliberately) and rather slow (because it manually interprets the abstract syntax tree). It can only handle Python syntax, and you can't have more than one value per key in Python dictionaries. – Martijn Pieters Jan 26 '19 at 14:34
  • See the duplicate on how you actually can parse JSON with repeated keys. Note that this is actually a violation of the JSON standard too, JSON objects must not have repeated keys *either*. – Martijn Pieters Jan 26 '19 at 14:34
  • (apologies, duplicate targets updated, I had picked a Javascript one first) – Martijn Pieters Jan 26 '19 at 14:35
  • @MartijnPieters thank you, that's perfect :) – Henry Cooper Jan 26 '19 at 14:42

0 Answers0