How to dump json without quotes in python

后端 未结 5 2175
孤街浪徒
孤街浪徒 2021-02-14 17:59

Here is how I dump a file

with open(\'es_hosts.json\', \'w\') as fp:
  json.dump(\',\'.join(host_list.keys()), fp)

The results is



        
5条回答
  •  猫巷女王i
    2021-02-14 18:25

    Well, that's not valid json, so the json module won't help you to write that data. But you can do this:

    import json
    
    with open('es_hosts.json', 'w') as fp:
        data = ['a', 'b', 'c']
        fp.write(json.dumps(','.join(data)).replace('"', ''))
    

    That's because you asked for json, but since that's not json, this should suffice:

    with open('es_hosts.json', 'w') as fp:
        data = ['a', 'b', 'c']
        fp.write(','.join(data))
    

提交回复
热议问题