python--working with JSON files

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.