Reading and Writing JSON through Python

前端 未结 2 1104
予麋鹿
予麋鹿 2020-12-06 20:30

read.json file :

{
    \"Username\" : \"admin\",
    \"Password\" : \"admin\",
    \"Iterations\" : 5,
    \"Decimal\" : 5.5,
    \"tags\" : [\"hello\", \"b         


        
2条回答
  •  南方客
    南方客 (楼主)
    2020-12-06 21:06

    you can directly dump json data to file. Docs

    import json
    with open('read.json', 'w') as outfile:
        json.dump(data, outfile, sort_keys=True, indent=4)
        # sort_keys, indent are optional and used for pretty-write 
    

    To read json from file:

    with open('read.json') as data_file:    
        data = json.load(data_file)
    

提交回复
热议问题