0

I have a JSON file that looks like this:

{
    "options": {
        "--singleseed": "Eugene Ionesco",
        "--booktitle": "Rhinoceros"
    }
}

I am loading it via

with open('/tmp/pagekicker/test.json') as json_data:
     d = json.load(json_data)
     s = str(d)

I now want to convert this to the following string:

--singleseed "Eugene Ionesco" --booktitle "Rhinoceros"

How can I do this?

1 Answer 1

2

Iterate over the dictionary. Build a string

data = {
    "options": {
        "--singleseed": "Eugene Ionesco",
        "--booktitle": "Rhinoceros"
    }
}

s = ' '.join( '{} "{}"'.format(k, v) for k,v in data['options'].items() )

print(s)  # --singleseed "Eugene Ionesco" --booktitle "Rhinoceros"
Sign up to request clarification or add additional context in comments.

1 Comment

This does not seem to produce the quotes around the values, e.g., "Eugene Ionesco", which are important. ' with open('/tmp/pagekicker/test.json') as json_data: d = json.load(json_data) s = "" for k,v in d['options'].items(): s += (2*"{} ").format(k, v) print(s) cli = commandpath + ' ' + s print('cli will be ' + cli) args = shlex.split(cli) print(args) ' produces --booktitle Rhinoceros --singleseed Eugene Ionesco

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.