Reprint Source:61198555Import JSON
Import Osdef opera_file1 (): document = open ("Testfile.txt", "w+") # print ("File name:", Document.name ) document.write ("This is the first test file I created! \nwelcome! ") Print (Document.tell ()) # Output current pointer position Document.seek (OS. Seek_set) # set pointer back to file initially context = Document.read () Print (context) Docum Ent.close () def opera_file2 (str_content): with open ("Testfile.txt", "w+") as f: F. Write (str_content) # Read {dictionary} type JSON file: # set to read the file in Utf-8 decoding mode, encoding parameter must be set, otherwise the default in GBK mode to read the file, when the file contains Chinese, will error Def json_dict () : f = open ("Repositories.json", encoding= ' Utf-8 ') setting = Json.load (f) # Note the read of multiple structures Take grammar family = setting[' basesettings ' [' Font '] style = setting[' fontFamily '] print ( Family) print (style) # Read JSON file in "list" format # load data into a list def json_list (): filename = ' c:/users/admin Istrator/desktop/123.json ' # Note 1: Absolute path notation temp_content = ' with open (filename) as f: Pop_data = Json.load (f) # Print the population of 2010 per country for Pop_dict in pop_data: country_name = pop_dict[' country name '] &NBSP ; population = pop_dict[' Value '] Temp_content + country_name + ': ' + population + '; \ n ' # print (country_name + ":" + population) &NB Sp Opera_file2 (temp_content) # print (temp_content) # Print out the final JSON string json_list () # json_dict ()
python--working with JSON files