Python:json Library &. json file read-write __js

Source: Internet
Author: User
Tags assert
API
API Annotation
Json.dumps () Converts a dictionary in Python to a string
Json.loads () Convert a string to a dictionary
Json.dump (,) Write data to the JSON file
Json.load () To open the file and transform the string into a data type
Test
Import JSON

dict_1 = {' City ': ' Nanjing ', ' Year ': 2018, ' Province ': ' Jiangshu '}


# dumps: Convert a dictionary in Python to a string
str_1 = Json.dumps (dict_1)
assert isinstance (str_1, str)

# Loads: Convert a string to a dictionary
dict_2 = json.loads (str_1) 
  
   assert isinstance (dict_2, dict)

# Dump: Writes data to the JSON file
with open ("./test.json", "W") as Dump_f:
    json.dump ( Dict_1, Dump_f)

# Load: Opens the file and transforms the string to the data type with
open ("./test.json", "R") as Load_f:
    dict_3 = Json.load ( Load_f)
    assert Isinstance (Dict_3, Dict)
  

[1] Python reads and writes 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.