Preface: as test engineers know, the json format file so that we commonly use a form of data storage, then for the processing of python files, python language has a unique condition, the nature of the json is stored in the form of key-value pairs, which is very much like the python language in the dictionary, so there are a lot of dictionary form of functions and methods, is directly available to use! The
Today we first talk about writing python scripts to deal with the core functions of json, some scattered, later in the overall scripting.
1, json file read after the operation
json file after reading the operation and the operation of the dictionary, you can dict in the relevant built-in methods are inherited, but the json file will have a list form and the existence of a mixture of the form of the dict.
It is important to be aware of the distinction at this point.
The data structure of a json file follows the structure of a dict, a key-value pair.
(1) It is very easy to change the value.
json["id"]=3001
(2) The modifier keys are a little trickier.
The dict dictionary has no concepts or methods for modifying keys directly.
Modify the key: first delete the original key-value pairs, add new key-value pairs, pay attention to get the old key-value pairs of value, passed to the new key-value pairs of value
gainvalue:value=json['request']["session_id"] Deleting key-value pairs:json['request'].pop("session_id") New key-value pairs:json['request'].setdefault("operation_id",value)
2. python recursively paths all files in the folder
def jsonfilePath_read(inputfilePath, outfilepath): rightCount = 0 errorCount = 0 for file in (inputfilePath): try: print(file) read_jsonfile_change2(str(inputfilePath + '/' + file), outfilepath + '/' + file) print(file + 'Modified successfully') rightCount += 1 except: print(file + 'Failed to modify file in error') errorCount += 1 print('File modification complete ', rightCount, 'Successful document', errorCount, "Errors in documentation)
Explain the core code:
core code: for file in (inputfilePath): #file is the current path to all the file names, do not forget to use the path + '/' + file try: ....inputfilePath + '/' + file except: print().....
3, json file reading and writing a new file
Read once write once can achieve json formatting , to solve the problem of json in the txt file is a single line display .
# Read the file with open('file/', 'r', encoding='utf8') as f: json_data = (f) jsondict = json_data # Write new files with open('outfile/', 'w', encoding='utf8') as r: (jsondict, r, ensure_ascii=False, indent=4) print('New document generation complete')
Core Point Description:
Read, write encoding='utf8' to prevent encoding problems indent=4, seems to be empty 4 cells, aesthetically pleasing output
to this article on the python json formatting and dictionary of the relationship between the article is introduced to this, more related python json dictionary relationship content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!