Serialization and deserialization of Python interface Testing (iv)

Source: Internet
Author: User
Tags serialization appium

The author is boundless

In Python, serialization can be interpreted as translating Python's object encoding into a JSON-formatted string, which can be interpreted as: Decoding a JSON-formatted string into a Python data object.

In Python's standard library, the JSON library is specifically provided with the Pickle Library to handle this part.

First to learn the JSON library, import the JSON library is very simple, directly import JSON, the following concrete examples to illustrate the use of the JSON library for serialization and deserialization. The main methods of the JSON library are:

#!/usr/bin/env Python#coding:utf-8
Import JSON
Print json.__all__

See the main methods of the JSON library:

[' Dump ', ' dumps ', ' load ', ' loads ', ' jsondecoder ', ' Jsonencoder ']

We define a dictionary that serializes it into JSON-formatted strings through JSON, as shown in the implemented code:

#!/usr/bin/env Python#coding:utf-8
Import jsondict1={' name ': ' Wuya ', ' age ': ' Address ': ' Xian '}
The data type before print U ' is not serialized is: ', type (DICT1)
Print U ' pre-serialized data: ', Dict
#对dict1进行序列化的处理str1 =Json.dumps (dict1)
Print u ' serialized data type: ', type (STR1)
Print U ' serialized data is: ', str1

See the contents of the code output as above:

1234567 C:\Python27\python.exe D: / git / Python / Doc / index.py The data type before the is not serialized is: < type   ' dict ' ; pre-serialized data: { ' age ' :   ,   ' name ' :   ' Wuya ' ,   ' Address ' :   ' Xian ' } The serialized data type is: < type   ' str ' ; The serialized data is: { "age" :   all ,   "name" :   "Wuya" ,   Address :   "Xian" } Process finished with exit code  0

As you can see from the code above as well as the results, this is a serialized process, which simply translates the Python data type into a JSON-formatted string.

Down we deserialize the JSON-formatted string into a Python data object, see the code and output:

#!/usr/bin/env Python#coding:utf-8
Import JSON
dict1={' name ': ' Wuya ', ' age ': ' Address ': ' Xian '}
The data type before print U ' is not serialized is: ', type (DICT1)
Print U ' pre-serialized data: ', Dict1
#对dict1进行序列化的处理
Str1=json.dumps (Dict1)
Print u ' serialized data type: ', type (STR1)
Print U ' serialized data is: ', str1
#对str1进行反序列化
dict2=json.loads (str1)
Print U ' deserialized data type: ', type (DICT2)
Print U ' deserialized data: ', Dict2

See the contents of the output results:

C:\Python27\python.exe d:/git/python/doc/index.py

The data type before serialization is: <type ' dict ' > pre-serialized data: {' age ': ', ' name ': ' Wuya ', ' address ': ' Xian '} serialized data type: <type ' str ' > The serialized data is: {"Age": $, "name": "Wuya", "Address": "Xian"} deserialized data type: <type ' dict ' > deserialized data: {u ' age ': $, U ' name ': U ' W Uya ', U ' address ': U ' Xian '}

Here we combine the requests library to see the JSON data returned, the code is:

#!/usr/bin/env Python#coding:utf-8
Import JSON
Import Requestsr=requests.get (' Http://wthrcdn.etouch.cn/weather_mini?city= Xian ')
Print r.text,u ' data type: ', type (r.text)
#对数据进行反序列化的操作
Dic=json.loads (R.text)
Print dic,u ' data type: ', type (DIC)

See what the output says:

C:\Python27\python.exe d:/git/python/doc/index.py{"desc": "OK", "status": +, "data": {"Wendu": "3", "Ganmao": " Day and night temperature difference is larger, more prone to colds, please appropriate increase or decrease clothing.
Please pay attention to the protection of weak constitution. "," forecast ": [{" Fengxiang ":" Northeast Wind "," Fengli ":" Breeze Level "," High ":" Hot 10 ℃ "," type ":" Sunny "," Low ":" Cryogenic -2℃ "," Date ":" 22nd Thursday "},{" Fengxiang ":" Northeast "," Fengli ":" Breeze "," High ":" Hot 8 ℃ "," type ":" Cloudy "," Low ":" Cryogenic 0 ℃ "," date ":" Friday 23rd "},{" Fengxiang ":" Northeast "," Fengli ":" Breeze Level "," High ":" Hot 7 ℃ "," type ":" Yin "," low ":" Cold 0 ℃ "," date ":" 24th Saturday "},{" Fengxiang ":" Northeast Wind "," Fengli ":" Breeze "," higher ": "High Temperature 1 ℃", "type": "Sleet", "low": " -1℃", "Date": "25th Sunday"},{"Fengxiang": "Northeast Wind", "Fengli": "Breeze Level", "High": "Hot 5 ℃", "type": "Cloudy" , "Low": "Cryogenic 1 ℃", "date": "26th Monday"}], "Yesterday": {"fl": "Breeze", "FX": "North Wind", "High": "Hot 7 ℃", "type": "Yin", "low": "Cryogenic -1℃", "date" : "21st Wednesday"}, "AQI": "The", "City": "Xi ' an"} data type: <type ' Unicode ' >{u ' status ': $, U ' data ': {u ' city ': U ' \u897f\u5b89 ', U ' yesterday ': {u ' FX ': U ' \u5317\u98ce ', U ' type ': U ' \u9634 ', U ' high ': U ' \u9ad8\u6e29 7\u2103 ', U ' low ': U ' \u4f4e\u6e29-1\ U2103 ', U ' date ': U ' 21\u65e5\u661f\u671f\u4e09 ', U ' fl ': U ' \u5fae\u98ce '}, U ' forecast ': [{u ' Fengxiang ': U ' \u4e1c\u5317\ U98ce ', U ' high ': U ' \u9ad8\u6e29 10\u2103 ', U ' Fengli ': U ' \u5fae\u98ce\u7ea7 ', U ' date ': U ' 22\u65e5\u661f\u671f\u56db ', U ' type ': U ' \u6674 ', U ' low ': U ' \u4f4e\u6e29-2\u2103 '}, {u ' Fengxiang ': U ' \u4e1c \u5317\u98ce ', U ' high ': U ' \u9ad8\u6e29 8\u2103 ', U ' Fengli ': U ' \u5fae\u98ce\u7ea7 ', U ' date ': U ' 23\u65e5\u661f\u671f\ U4e94 ', U ' type ': U ' \u591a\u4e91 ', U ' low ': U ' \u4f4e\u6e29 0\u2103 '}, {u ' Fengxiang ': U ' \u4e1c\u5317\u98ce ', U ' high ': U ' \ U9ad8\u6e29 7\u2103 ', U ' Fengli ': U ' \u5fae\u98ce\u7ea7 ', U ' date ': U ' 24\u65e5\u661f\u671f\u516d ', U ' type ': U ' \u9634 ', U ' Low ': U ' \u4f4e\u6e29 0\u2103 '}, {u ' Fengxiang ': U ' \u4e1c\u5317\u98ce ', U ' high ': U ' \u9ad8\u6e29 1\u2103 ', U ' Fengli ': U ' \ U5fae\u98ce\u7ea7 ', U ' date ': U ' 25\u65e5\u661f\u671f\u5929 ', U ' type ': U ' \u96e8\u5939\u96ea ', U ' low ': U ' \u4f4e\u6e29-1 \u2103 '}, {u ' Fengxiang ': U ' \u4e1c\u5317\u98ce ', U ' high ': U ' \u9ad8\u6e29 5\u2103 ', U ' Fengli ': U ' \u5fae\u98ce\u7ea7 ', U ' Date ': U ' 26\u65e5\u661f\u671f\u4e00 ', U ' type ': U ' \u591a\u4e91 ', U ' low ': U ' \u4f4e\u6e29 1\u2103 '}], U ' Ganmao ': U ' \u663c \u591c\u6e29\u5dee\u8f83\u5927\uff0c\u8f83\u6613\u53d1\u751F\u611f\u5192\uff0c\u8bf7\u9002\u5f53\u589e\u51cf\u8863\u670d\u3002\u4f53\u8d28\u8f83\u5f31\u7684\u670b\u53cb\ U8bf7\u6ce8\u610f\u9632\u62a4\u3002 ', U ' wendu ': U ' 3 ', U ' AQI ': U ' '}, u ' desc ': U ' OK '} data type: <type ' dict ' >process Finished with exit code 0

In fact, in the code above, we can do this without deserialization, and the code can be simplified to the following:

#!/usr/bin/env Python#coding:utf-8
Import JSON
Import Requestsr=requests.get (' Http://wthrcdn.etouch.cn/weather_mini?city= Xian ')
R.json (),u ' data type: ', type (R.json ())

See what the output says:

C:\Python27\python.exe d:/git/python/doc/index.py{u ' status ': +, U ' data ': {u ' city ': U ' \u897f\u5b89 ', U ' Yesterday ': {u ' FX ': U ' \u5317\u98ce ', U ' type ': U ' \u9634 ', U ' high ': U ' \u9ad8\u6e29 7\u2103 ', U ' low ': U ' \u4f4e\u6e29-1\u2103 ', U ' date ' : U ' 21\u65e5\u661f\u671f\u4e09 ', U ' fl ': U ' \u5fae\u98ce '}, U ' forecast ': [{u ' Fengxiang ': U ' \u4e1c\u5317\u98ce ', U ' high ' : U ' \u9ad8\u6e29 10\u2103 ', U ' Fengli ': U ' \u5fae\u98ce\u7ea7 ', U ' date ': U ' 22\u65e5\u661f\u671f\u56db ', U ' type ': U ' \ u6674 ', U ' low ': U ' \u4f4e\u6e29-2\u2103 '}, {u ' Fengxiang ': U ' \u4e1c\u5317\u98ce ', U ' high ': U ' \u9ad8\u6e29 8\u2103 ', U ' Fengli ': U ' \u5fae\u98ce\u7ea7 ', U ' date ': U ' 23\u65e5\u661f\u671f\u4e94 ', U ' type ': U ' \u591a\u4e91 ', U ' low ': U ' \u4f4e\ U6e29 0\u2103 '}, {u ' Fengxiang ': U ' \u4e1c\u5317\u98ce ', U ' high ': U ' \u9ad8\u6e29 7\u2103 ', U ' Fengli ': U ' \u5fae\u98ce\ U7ea7 ', U ' date ': U ' 24\u65e5\u661f\u671f\u516d ', U ' type ': U ' \u9634 ', U ' low ': U ' \u4f4e\u6e29 0\u2103 '}, {u ' Fengxiang ': U ' \u4e1c\u5317\u98ce ', U ' high ': U ' \u9ad8\u6e29 1\u2103 ', U ' Fengli ':U ' \u5fae\u98ce\u7ea7 ', U ' date ': U ' 25\u65e5\u661f\u671f\u5929 ', U ' type ': U ' \u96e8\u5939\u96ea ', U ' low ': U ' \u4f4e\ U6e29-1\u2103 '}, {u ' Fengxiang ': U ' \u4e1c\u5317\u98ce ', U ' high ': U ' \u9ad8\u6e29 5\u2103 ', U ' Fengli ': U ' \u5fae\u98ce\ U7ea7 ', U ' date ': U ' 26\u65e5\u661f\u671f\u4e00 ', U ' type ': U ' \u591a\u4e91 ', U ' low ': U ' \u4f4e\u6e29 1\u2103 '}], U ' Ganmao ' : U ' \u663c\u591c\u6e29\u5dee\u8f83\u5927\uff0c\u8f83\u6613\u53d1\u751f\u611f\u5192\uff0c\u8bf7\u9002\u5f53\ U589e\u51cf\u8863\u670d\u3002\u4f53\u8d28\u8f83\u5f31\u7684\u670b\u53cb\u8bf7\u6ce8\u610f\u9632\u62a4\u3002 ', u ' Wendu ': U ' 3 ', U ' AQI ': U ' '}, u ' desc ': U ' OK '} data type: <type ' Dict ' >process finished with exit code 0

In the actual work, the serialization or deserialization may be a form of a file, it is not as simple as the write down to implement this part, the file content is serialized and deserialized, first look at the serialized code:

#!/usr/bin/env Python#coding:utf-8
Import JSON
list1=[' Selenium ', ' appium ', ' Android ', ' iOS ', ' Uiautomator ']
#把list1先序列化, and then write to a file
Json.dump (List1,open (' C:/log.log ', ' W '))
Print u ' file contents: ' R=open (' c:/log.log ', ' r+ ')
Print R.read ()

See what the output says:

C:\Python27\python.exe D:/git/python/doc/index.pynone file contents are: ["Selenium", "Appium", "Android", "ios", "Uiautomator"] Process finished with exit code 0

Let's deserialize, that is, to read the contents of the file, and then deserialize, see the implementation of the Code:

#!/usr/bin/env Python#coding:utf-8
Import JSON
list1=[' Selenium ', ' appium ', ' Android ', ' iOS ', ' Uiautomator ']
#把list1先序列化, and then write to a file
Print Json.dump (List1,open (' C:/log.log ', ' W '))
Print u ' file contents: ' R=open (' c:/log.log ', ' r+ ')
Print R.read ()
#先读取文件内容, then deserialize
res=json.load (open (' C:/log.log ', ' r+ '))
Print res,u ' data type: ', type (res)

See what the output says:

C:\Python27\python.exe D:/git/python/doc/index.pynone file contents are: ["Selenium", "Appium", "Android", "ios", "Uiautomator"] [ U ' selenium ', U ' appium ', U ' android ', U ' iOS ', U ' uiautomator '] data type: <type ' list ' >process finished with exit code 0

Come down to see the Pickle Library, which provides the following methods:

#!/usr/bin/env Python#coding:utf-8
Import Pickle

Print pickle.__all__
C:\Python27\python.exe d:/git/python/doc/index.py[' pickleerror ', ' picklingerror ', ' unpicklingerror ', ' Pickler ', ' Unpickler ', ' dump ', ' dumps ', ' load ', ' loads ', ' APPEND ', ' appends ', ' binfloat ',


' Long_binget ', ' long_binput ', ' MARK ', ' newfalse ', ' NEWOBJ ', ' newtrue ', ' NONE ', ' OBJ ', ' persid ', ' POP ', ' Pop_mark ', ' PROTO ' ' PUT ', ' REDUCE ',
' SetItem ', ' setitems ', ' short_binstring ', ' STOP ', ' STRING ', ' TRUE ', ' TUPLE ', ' TUPLE1 ', ' TUPLE2 ', ' TUPLE3 ', ' UNICODE '] Process finished with exit code 0

Here we only focus on the Pickle Library's dump (), dumps (), load (), loads () method, first look at the serialized code:

Import pickledic={' name ': ' Infinite ', ' age ': ' Address ': ' XI ' an '}str1=pickle.dumps (DIC)
Print Str1,type (STR1)

See what the output says:

C:\Python27\python.exe d:/git/python/doc/index.py (dp0s ' age ' p1i22ss ' name ' p2s ' \xe6\x97\xa0\xe6\xb6\xaf ' p3ss ' Address ' p4s ' \xe8\xa5\xbf\xe5\xae\x89 ' p5s. <type ' str ' >

The output of the content is basically not understand, but you can see the data format is a string, which is the ASCII format of the data, the default is the ASCII format to save the object,

In the use of serialization, set to True is a binary save object, see the code and output of the implementation:

#!/usr/bin/env Python#coding:utf-8
Import pickledic={' name ': ' Infinite ', ' age ': ' Address ': ' XI ' an '}str1=pickle.dumps (dic,true)
Print Str1,type (STR1)

See the binary data for the output:

C:\Python27\python.exe d:/git/python/doc/index.py}q (uageqkunamequ quaddressqu Xi ' an qu. <type ' str ' >

The following is the loads () method to deserialize, see the code implemented:

#!/usr/bin/env Python#coding:utf-8
Import pickledic={' name ': U ' infinite ', ' age ': +, ' address ': U ' Xian '}
#序列化
Str1=pickle.dumps (Dic,true)
Print Str1,type (STR1)
#反序列化
dict1=pickle.loads (str1)
Print Dict1,type (DICT1)

See what the output says:


<type ' Dict ' >

Down we serialize and deserialize the file in the form of a document, see the code implemented:

#!/usr/bin/env Python#coding:utf-8
Import pickledic={' name ': U ' infinite ', ' age ': +, ' address ': U ' Xian '}
#先序列化, and then write to the file Pickle.dump (Dic,open (' C:/log.log ', ' W '), True)
The contents of the print U ' file are: '
Print open (' C:/log.log '). Read ()
#先读取文件, re-serialize
D=pickle.load (Open (' C:/log.log ', ' r+ '))
Print U ' deserialized data and data type: ', D,type (d)

See what the output says:


<type ' Dict ' >process finished with exit code 0

Serialization and deserialization of Python interface Testing (iv)

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.