JSON data processing: Reading a JSON string from a file to a Python dictionary

Source: Internet
Author: User

Method 1:

Read the JSON string in the file,

Then use Json.loads to convert to Python dictionary

Import Jsonstr_file = './960x540/config.json ' with open (Str_file, ' R ') as F:    print ("Load str file from {}". Format (str_ file))    str1 = F.read ()    r = json.loads (str1) print (Type (r)) print (r) print (r[' under_game_score_y '])

  

Method 2:

Directly with the file cursor F, the JSON string is completed in one step, along with reading and turning into a python dictionary. This is Josn.load (f).

Import Jsonstr_file = './960x540/config.json ' with open (Str_file, ' R ') as F:    print ("Load str file from {}". Format (str_ file))    r = json.load (f) Print (type (r)) print (r) print (r[' under_game_score_y ')

  

Conclusion:

The difference between loads and load in the JSON module is:

Loads is to read the string in the F cursor first, and convert the string into a python dictionary

Load is one step the file cursor f into a python dictionary.

JSON data processing: Reading a JSON string from a file to a Python dictionary

Related Article

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.