简体   繁体   中英

Convert python3 JSON to command line string

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?

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM