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

Source: Internet
Author: User

#Auther Bob
#--*--conding:utf-8--*--

#jshon这个模块就是做序列化处理的, 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这个文件中

# 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

1234567 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>)

  

Shelve can also be opened with the way
123456789101112131415161718192021 import shelvewith shelve.open("test_shelve.db") as f:    f["k1"] = {        "name":"zhangguojun1",        "age":12,        "address":"shenzhen1"    }    f["k2"] = {        "name":"zhangguojun2",        "age":12,        "address":"shenzhen2"    }    f["k3"] = {        "name":"zhangguojun3",        "age":12,        "address":"shenzhen3"    }    f["k4"] = ["张国军1","张国军2","张国军3"]with shelve.open("test_shelve.db") as f:    print(f["k1"]["name"])    print(f["k4"][0])

  

The results are as follows

12 zhangguojun1张国军1

  

123456789 with shelve.open("shelve.ini","w") as f:    f["k1"] = test_list    f["k2"] = test_dict    f["k3"] = swith shelve.open("shelve.ini","r") as f:    print(f["k3"])    print(f["k2"])    print(f["k1"])

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

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.