Python--json string related loads dumps load dump

Source: Internet
Author: User

# 1 JSON string length like dictionary, but not dictionary type, is str type
#例如: User_info is a JSON string, Dict is a dictionary, if the content of the TXT text that identifies dict is a JSON string
User_info = "'
{"name1": "Lily", "name2": "tt", "Name3": "EE"}
‘‘‘
Dict = {"name1": "Lily", "name2": "tt", "Name3": "EE"}
#2, JSON string content such as double quotation marks, cannot use single quotation marks, dictionary single double quotation marks can be
Convert #3, loads strings to dictionaries (loads to single quotes after conversion to dictionaries), dumps dictionaries to strings
#4, www.bejson.com Web site Verify that the string can be converted using JSON
The #5, dumps, and dump methods dumps convert the dictionary to a string, do not write, dump converts the dictionary into a string, writes
# dump () format dump (dictionary, sentence grapple)
#例如
Import JSON
Dict_user = {"name1": "Lily", "name2": "tt", "Name3": "EE"}
With open ("Usertest.txt", "w+", encoding= "Utf-8") as FW:
Json.dump (dict_user,fw,indent=4)
#6, indent indentation in JSON format, generally as dump or dumps method parameter for TXT text JSON format indentation
# to change the txt file suffix to JSON can also implement indentation and color hints

#7, loads (str) converts a string to a dictionary passing a string
# load (handle) converts a string in a file into a dictionary pass a file handle

#例如:
Import JSON
With open ("Users2.txt", "r+", encoding= "Utf-8") as FW:
data = Fw.read ()
Print (data)
User_dict=json.loads (data)
Print (user_dict)
With open ("Users2.txt", "r+", encoding= "Utf-8") as FW:
User_dict2=json.load (FW)
Print (USER_DICT2)

Python--json string related loads dumps load dump

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.