Source:
. 1 Import OS 2 Import JSON . 3 . 4 . 5 class SaveJson (Object): . 6 . 7 DEF ! Save_file (Self, path, Item): . 8 . 9 # first dictionary object can be written into a string of text 10 Item = json.dumps ( Item) . 11 12 is the try : 13 is IF Not os.path.exists (path): 14 with Open (path, " W " , encoding = ' UTF-. 8 ' ) AS F: 15 f.write (+ Item ",\n") 16 print("^_^ write success") 17 else: 18 with open(path, "a", encoding='utf-8') as f: 19 f.write(item + ",\n") 20 print("^_^ write success") 21 except Exception as e: 22 print("write error==>", e) 23 24- 25 IF __name__ == ' __main__ ' : 26 # save file name 27 path = " test1.json " 28 # Case dictionary data 29 Item = { " uid " : " 5bc05421vbjgj34hj9c7d83 " , " oss_status_code " : 200 , 30 " url " : " https://ssyerv2.oss-cn-hangzhou.aliyuncs.com//picture/zl/687122.jpg " , 31 "updatedAt": "1970-01-18", "createdAt": "1970-01-18", "PID": "5b923c7vbcvbxcswrw342504b", 32 "_id": "5b98d052ed0cbe41","CID":"afdsfgasgfafghdgssdhh"inIfor37 [test code write cycle three lines, there is no blank line#3635SaveJson () S =34 is33 is} range(3): 38 s.save_file(path, item)
operation result:
document content: