[] The python json string written to the file format and read out in a python json json achieve txt file data is written to write python json json format file

写json--json.dumps

Code:

import json

# To be written to the file json string (dict format) 
Result = { ' ErrorTest ' : [( ' 63 is ' , ' rental repair XXX ' , ' 400 ' , ' https://appapi.xxxx.com/appapi/brainchoosehouse/ V1 / updaterent ' , ' requested data error check [request data] whether the field is a standard format json ' , ' Cao ' )],
              ' run_data ' : { ' stime ' : ' 2019-10-10 10: 22:39 ' , 'fail_case': 1, 'etime': '2019-10时00分00秒', 'all_case': 1341, 'jump_case': 176, 'run_case': 1165, 'pass_case': 1164}}

# Write the file to json format 
with Open ( " result.txt " , " w " ) AS fp:
    fp.write(json.dumps(result,indent=4))

 

effect:

 

Here to see Chinese json string into a unicode string

In order to turn into Chinese unicode string

It can be written:

# Ensure_ascii = False not represented in ascii code written to the file format 
with Open ( ' the result.txt ' , ' W ' ) AS FP:
    fp.write(json.dumps(result,indent=4,ensure_ascii=False))

effect:

 

读json-json.load

with open('result.txt','r') as load_f:
    load_dict=json.load(load_f)
    

print(type(load_dict),load_dict)

# Results: 
< class  ' dict ' > { ' ErrorTest ' : [[ ' 63 is ' , ' rental option xxxx room card ' , ' 400 ' , ' https://appapi.xxxx.com/appapi/brainchoosehouse/v1/updaterent ' , ' requested data error check [request data] whether the field is a standard format json ' , ' John Doe ' ]], ' run_data ' : { ' stime ' : ' 2019-10-10 10:22:39', 'fail_case': 1, 'etime': '2019-10时00分00秒', 'all_case': 1341, 'jump_case': 176, 'run_case': 1165, 'pass_case': 1164}}

 

Reference documents:

python json data is written to achieve the txt file to json format 

python json file to read and write

 

 

Guess you like

Origin www.cnblogs.com/kaerxifa/p/11652898.html