Introduction to the Dumps,loads,dump,load method for Python JSON modules

Source: Internet
Author: User

Both load and loads are implemented as "deserialization", the difference being in Python (for example):
Loads the python built-in data into a string for memory objects
such as using Json.dumps serialized object D_json=json.dumps ({' A ': 1, ' B ': 2}), where D_json is a string ' {"B": 2, "a": 1} '
D=json.loads (D_json) #{B ": 2," a ": 1}, re-deserialized to dict using load
Load for file handle

If there is a JSON file locally A.json you can d=json.load (open (' A.json '))
Accordingly, dump is to serialize the built-in type to a JSON object and write to the file

#--*--conding:utf-8--*--
#json这个模块就是做序列化处理的, four ways to use the JSON module mainly
#1, Dumps
#2, loads
#3, Dump
#4, Load

#先介绍dumps方法
#通过jshon的dumps的模块可以把特定的对象序列化处理为字符串
# import JSON

# L1 = [1,2,3,454]
# d1 = {' K1 ': ' v1 '}
# ret = json.dumps (L1)
# Print (Type (ret))
# ret = json.dumps (d1)
# Print (Type (ret))

# <class ' str ' >
# <class ' str ' >

# l1 = ' [1,2,3,4] '
# D1 = ' {"K1": "v1"} '
# Print (Type (L1))
# Print (Type (D1))

#在来介绍loads方法
#上面的l1和d1都是字符串, but they look like List and dict, and we can convert these 2 strings to list and dict by deserializing, if
#外形不是list或者dict的形状, it is not successful, it must be noted that the outer quotation marks of the string must be "single quotation marks", internal must be double quotation marks, if not, the JSON module will error
# ret = json.loads (L1)
# Print (Ret,type (ret))
# ret = json.loads (d1)
# Print (Ret,type (ret))


# [1, 2, 3, 4] <class ' list ' >
# {' K1 ': ' v1 '} <class ' Dict ' >


#来做一个小练习, through the third-party module get to the HTTP request, and then the JSON module returns the string structure of the data into the form of a dictionary, so that we can
#对这个字典做操作
# import Requests
# import JSON
#
# ret = requests.get (' http://wthrcdn.etouch.cn/weather_mini?city= Beijing ')
# ret.encoding = ' Utf-8 '
# S1 = Ret.text
# Print (S1,type (S1))

#拿到字符串形式的数据
# {"desc": "Invilad-citykey", "Status": 1002} <class ' str ' >
#
# D1 = json.loads (S1)
# Print (D1,type (D1))

#通过loads的方法, convert the string into a dictionary
# {' desc ': ' Invilad-citykey ', ' Status ': 1002} <class ' Dict ' >

#上面的dumps和loads方法都在内存中转换, the following dump and load methods will be one more step, dump is to write the serialized string into a file, and
#load是从一个一个文件中读取文件

#然后来介绍dump方法
# import JSON
# d1 = {' name ': ' Foot '}
#这一步就会把d1做序列化处理后的字符串写到db这个文件中

#with Open (' Data.json ') as Json_file:
# data = Json.load (json_file)
# Json.dump (D1,open (' db ', ' W '))
# D1 = json.load (open (' db ', ' R '))
# Print (D1,type (D1))

# {' name ': ' Foot '} <class ' Dict ' >


today I learned the shelve module, this module is still very simple to use
ImportSHELVEF = Shelve.open ("Shelve_test") f[‘Info'] ="Alex"f["Age"] = [1,34,5,6,33,44]f["Name"] = {"Name":"Alex","Add":"Sz"}f.close () F = Shelve.open ("Shelve_test")#Here this f can be understood as a Dictionary object, so shelve stores data as a dictionary store dataPrint (F.get ( "info" print (F.get (" age "print (F.get (" name  ")) print (F[ ' info ' ]) print  (F.values ()) print ( F.items ()) print (F.keys ())         

The results are as follows

1 2 3 4 5 6 7 alex[1, 34, 5, 6, 33, 44]{‘name‘: ‘alex‘, ‘add‘: ‘sz‘}alexValuesView(<shelve.DbfilenameShelf object at 0x019EB250>)ItemsView(<shelve.DbfilenameShelf object at 0x019EB250>)KeysView(<shelve.DbfilenameShelf object at 0x019EB250>)

Introduction to the Dumps,loads,dump,load method for Python JSON modules

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.