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

Source: Internet
Author: User

#--*--conding:utf-8--*--#jshon这个模块就是做序列化处理的, the main use of the JSON module four ways, Dumps#2, loads#3, Dump#4, load# first introduced dumps method, The dumps module of Jshon can be used to serialize specific objects into string # import json# l1 = [1,2,3,454]# d1 = {' K1 ': ' v1 '}# ret = json.dumps (L1) # print (ret) # ret = json.dumps (d1) # print (ret) # <class ' str ' ># <class ' str ' ># l1 = ' [1,2,3,4] ' # d1 = ' {' K1 ': ' v1 '} ' # Print (Type (L1)) # Print (Type (D1)) # #在来介绍loads方法 # The above L1 and D1 are strings, but they look like the list and the dict, and we can convert the 2 strings to list and dict by deserialization, Here if the shape is not list or dict, then the successful # ret = Json.loads (L1) # Print (Ret,type (ret)) # ret = json.loads (d1) # Print (Ret,type (ret)) is not converted # [1, 2, 3, 4] <class ' list ' ># {' K1 ': ' v1 '} <class ' Dict ' > #来做一个小练习, via third-party module get to HTTP request, The JSON module then converts the data of the returned string structure into the form of a dictionary, so that we can do the operation on this dictionary # 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的方法, put the wordstring into 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 is to read the file from a file # and then to introduce the Dump method # import json# d1 = {' name ': ' Foot '}# This step will write the D1 serialized string into the db file # Json.dump (D1,open (' db ', ' W ')) # D1 = json.load (open (' db ', ' R ')) # print (D1,type (D1)) # {' Name ': ' Foot '} <class ' Dict ' >
Pickle.dump (info,f) #f. Write (Pickle.dumps (info)) (The difference)

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.