Python Development Skills required (4)

Source: Internet
Author: User
Tags serialization

7th place: Json&pickle (serialization and deserialization module)

Reference Java URL: http://blog.csdn.net/a2011480169/article/details/51771539

Introduction: The role of the eval built-in function in Python:

Eval () is a python built-in tool function that converts a string object into a valid expression to participate in an evaluation operation and returns the result of the calculation.

Eval () allows you to convert between a string object and a dictionary, a list, and a tuple object.

code example:

Str_info1 = "(' Python ', ' Java ', ' Scala ')" Print (Str_info1,type (STR_INFO1)) print (eval (str_info1), type (eval (str_info1 )) Str_info2 = "[' Python ', ' Java ', ' Scala ']" Print (Str_info2,type (Str_info2)) print (eval (str_info2), type (eval (str_ Info2)) Str_info3 = ' {' name ': ' Python ', ' price ': ' $ ' Print (Str_info3,type (STR_INFO3)) print (eval (STR_INFO3), type ( Eval (STR_INFO3)))

Operation Result:

(' Python ', ' Java ', ' Scala ') <class ' str ' > (' Python ', ' Java ', ' Scala ') <class ' tuple ' >[' python ', ' Java ', ' Scala '] <class ' str ' >[' python ', ' Java ', ' Scala '] <class ' list ' >{' name ': ' Python ', ' price ': ' $ ' <class ' Str ' >{' name ': ' Python ', ' price ': $ <class ' dict ' >process finished with exit code 0

Eval () flaw in the built-in function:

Eval () is used to extract the data types in a string, but the Eval method is limited and can be used for normal data types, json.loads and eval, but when it comes to special types, eval doesn't work. So the focus of eval is usually to execute a string expression and return the value of the expression.

code example:

This example can be changed as follows:

Import Jsonx = "[none,true,false,1]" Print (eval (x))

Two modules for serialization:

JSON, used to convert between string and Python data types

Pickle for conversion between Python-specific types and Python data types

The JSON module provides four functions: dumps, dump, loads, load

The Pickle module provides four functions: dumps, dump, loads, load

JSON provides a common way to serialize and deserialize:

Concepts of serialization and deserialization: (important)

Converting an in-memory data type to a string or bytes type (binary form), called serialization (JSON, Pickle, serialization); After serialization, you can put the sequence

The content is written to disk or transmitted over the network to another machine. Converts a string or bytes type (binary form) into an in-memory data type called deserialization.

In Python, your own serialization and deserialization are pickle, and the code looks like this:

code Example 1:

Import pickleuser_info = {    ' name ': ' Angele ',    ' age ': +,    ' city ': ' Beijing ',}print (User_info,type (user_ info)) #python特有的序列化方式:p ickle to convert data types in Python to Bytesp_bytes = Pickle.dumps (user_info) print (P_bytes,type (p_bytes)) # Python-specific deserialization: pickle, converting bytes to data type in Python user_info = pickle.loads (p_bytes) print (user_info)

Operation Result:

{' name ': ' Angele ', ' age ': +, ' city ': ' Beijing '} <class ' dict ' >b ' \x80\x03}q\x00 (x\x04\x00\x00\x00nameq\x01x\x06 \x00\x00\x00angeleq\x02x\x03\x00\x00\x00ageq\x03k#x\x04\x00\x00\x00cityq\x04x\x07\x00\x00\x00beijingq\x05u. ' <class ' bytes ' >{' name ': ' Angele ', ' age ': +, ' city ': ' Beijing '}

code Example 2: Writing serialized data from Python to disk, and deserializing read (recommended way)

Combination of dumps and loads

Import pickleuser_info = {    ' name ': ' Angele ',    ' age ': +,    ' city ': ' Beijing ',}with open (' Example.pkl ', ' WB ') As FW:    print (Pickle.dumps (user_info))    Fw.write (Pickle.dumps (user_info)) with open (' example.pkl ', ' RB ') as FR :    user_info = Pickle.loads (Fr.read ())    print (User_info,type (user_info))

Operation Result:

B ' \x80\x03}q\x00 (x\x04\x00\x00\x00nameq\x01x\x06\x00\x00\x00angeleq\x02x\x03\x00\x00\x00ageq\x03k#x\x04\x00\ x00\x00cityq\x04x\x07\x00\x00\x00beijingq\x05u. ' {' name ': ' Angele ', ' age ': +, ' city ': ' Beijing '} <class ' Dict ' >process finished with exit code 0

code example 3: writing serialized data from Python to disk, and deserializing read (using file handles, not recommended) 

The combination of dump and load uses

Import pickleuser_info = {    ' name ': ' Angele ',    ' age ': +,    ' city ': ' Beijing ',}with open (' Example.pkl ', ' WB ') As FW:    Pickle.dump (USER_INFO,FW) with open (' example.pkl ', ' RB ') as fr:    print (Pickle.load (FR)) "" "" {' Name ': ' Angele ', ' age ': +, ' city ': ' Beijing '}process finished with exit code 0 ""

The method described above is a python-specific way of serializing and deserializing, followed by a general approach to serialization and deserialization.

How JSON is serialized and deserialized:

code Example 1:

Import jsonuser_info = {    ' name ': ' Angele ',    ' age ': +,    ' city ': ' Beijing ',} #Json序列化: Convert Python data type to string str_ JS = Json.dumps (user_info) print (Str_js,type (STR_JS)) #Json反序列化: Convert string to data type in Python user_info = json.loads (STR_JS) Print (User_info,type (user_info))

Operation Result:

{"City": "Beijing", "age": +, "name": "Angele"} <class ' str ' >{' city ': ' Beijing ', ' age ': +, ' name ': ' Angele '} <c Lass ' Dict ' >process finished with exit code 0

code Example 2: Writing serialized data from Python to disk, and deserializing read (recommended this common way)

Dumps and loads

Import jsonuser_info = {    ' name ': ' Angele ',    ' age ': +,    ' city ': ' Beijing ',} #序列化with open (' Example.json ', Encoding= ' Utf-8 ', mode= ' W ') as FW:    user_info = Json.dumps (user_info)    print (User_info,type (user_info))    Fw.write (User_info) #反序列化with open (' Example.json ', encoding= ' utf-8 ', mode= ' R ') as fr:    User_info = Json.loads ( Fr.read ())    print (User_info,type (user_info))

Operation Result:

{"Name": "Angele", "age": +, "City": "Beijing"} <class ' str ' >{' name ': ' Angele ', ' age ': +, ' city ': ' Beijing '} <c Lass ' Dict ' >process finished with exit code 0

Data in the example file:

code example 3: Writing serialized data from Python to disk, and deserializing read (using file handles, not recommended for this generic approach)

Combination of dump and load

Import jsonuser_info = {    ' name ': ' Angele ',    ' age ': +,    ' city ': ' Beijing ',} #序列化with open (' Example.json ', Encoding= ' Utf-8 ', mode= ' W ') as FW:    json.dump (USER_INFO,FW) #反序列化with open (' Example.json ', encoding= ' utf-8 ', mode= ' R ') as fr:    print (Json.load (FR))

The concept of JSON:

JSON is a lightweight data interchange format, essentially a string of strings.

If we were to pass objects between different programming languages, we had to serialize the objects into standard formats, such as XML, but a better approach would be to serialize JSON, because JSON represents

Out is a string that can be read by all languages, easily stored to disk, or transmitted over a network.

JSON is not only a standard format, but also faster than XML, and can be read directly in the Web page, very convenient. The most extensive application of JSON is as a Web server and customer in Ajax

Data format for the end of the communication.

The similarities and differences between JSON and pickle:

First: JSON operates as a string type when writing and reading data, and Pickle is operating in binary format when writing and reading data.

Second: JSON supports only a few simple common data types, such as strings, lists, dictionaries, Ganso, collections, etc. pickle supports simple data types and supports complex data

types, such as functions, classes, and so on. That is, pickle can support serializing all data types in Python.

Third: JSON can be converted across languages, and pickle can only be used in Python.

IV: JSON is a common way of serializing and deserializing, and Pickle is the way in which Python has its own unique serialization and deserialization.

Note: If you write data to a file, you can write only two formats:

First: Text strings (numbers must first become strings)

Second: bytes B ' \x80\x03}q\x00 (x\x02\x00\x00\x00i ' type.

Questions:

Actual combat:

JSON is essentially a JSON string, how do you understand it?

Str_info = "python" Print (Json.loads (str_info))

Valueerror:expecting value:line 1 column 1 (char 0)

8th place: Hashlib Encryption Module

This algorithm I used in the work of Django, in the verification of the file is also used.

the role of the hashlib algorithm in Python :

Print (Time.clock ())

Python's hashlib algorithm provides a common digest algorithm, such as MD5, SHA1, and so on, and the abstract algorithm is called hash algorithm and hash algorithm.

Abstract the algorithm is to calculate the fixed-length summary digest by using the Digest function f () for data of arbitrary length, in order to find out whether the original data has been tampered with by others.

Abstract the algorithm can indicate whether the data has been tampered with, because the digest function is a one-way function, the calculation of f (data) is very easy, but by digest data is very sleepy

Difficult. Also, making a bit change to the original data will result in a completely different summary of the calculations.

Methods used by the Hashlib module in Python:

First: Create a Hash object H = hashlib.md5 ()

Second: The byte object arg is populated into a hash object, and arg is typically the string to be encrypted

H.update (ARG)

Third: Call the Digest () or Hexdigest () method to get the digest (encrypted result)

H.hexdigest ()

Hashlib Verify the file principle:

If the same hash object repeatedly calls the update () method, then M.update (a); M.update (b) equivalent to M.update (A+B)

Example Program 1:

Import Hashlib The basic use of the hashlib algorithm in Python: "" "#第一: Create a Hash object, H=HASHLIB.MD5 () H = hashlib.md5 () #第二: Populates the Byte object arg with the hash object, Arg is typically the string to be encrypted h.update (' Angela '. Encode (' Utf-8 ')) #第三: Call the Hexdigest () method to get the digest (encrypted result) print (H.hexdigest ())

Operation Result:

36388794be2cf5f298978498ff3c64a2process finished with exit code 0

  

Example Program 2: (Validation of user names and passwords in Django)

Import Hashlib "" "Python Hashlib algorithm in Django similar application" "" user_name = input (' Please enter registered user name: ') Pass_word = input (' Please enter the registered password: ') Print (' \033[42m your registered username is:%s, the password is:%s\033[0m '% (User_name,pass_word)) m1 = Hashlib.md5 () m1.update (Pass_word.encode (' Utf-8 ') Pass_word_md5 = M1.hexdigest () print (PASS_WORD_MD5) #将用户注册后的信息写到数据库当中with open (' Db.txt ', encoding= ' utf-8 ', Mode= ' W ') as FW:    fw.write ('%s|%s '% (user_name,pass_word_md5)) #用户二次登陆进行用户名和密码校验username = input (' Please enter registered user name: ') Password = input (' Please enter registered password: ') m2 = hashlib.md5 () m2.update (Password.encode (' Utf-8 ')) Password_md5 = M2.hexdigest () with Open (' Db.txt ', encoding= ' utf-8 ', mode= ' R ') as fr:    user,pwd = Fr.read (). Split (' | ')    Print (USER,PWD)    if user = = Username and pwd = = Password_md5:        print (' login successful. ')    else:        print (' Login failed.. ')

  

Example Program 3: (* * *)

Import Hashlib The use of the Hashlib algorithm in the text check in Python: actually verifying the contents of the text string "" "M1 = Hashlib.md5 () m1.update (' Angela '. Encode (' Utf-8 ') ) m1.update (' Helloyou '. Encode (' Utf-8 ')) print (M1.hexdigest ()) m2 = Hashlib.md5 () m2.update (' Angelahelloyou '. Encode ( ' Utf-8 ')) print (M2.hexdigest ())

Operation Result:

Eb4ab8ec5815365e9fc88dfa444d9796eb4ab8ec5815365e9fc88dfa444d9796process finished with exit code 0

  

Sample program 4: (Checksum for text files)

Import Hashlib "" "in Python, the use of the Hashlib algorithm in text validation: actually verifying the contents of the text string" "" M = Hashlib.md5 () with open (' Db.txt ', encoding= ' utf-8 ' , mode= ' R ') as fr:    data = Fr.read ()    m.update (Data.encode (' Utf-8 ')) print (    m.hexdigest ()) print (' ********* ') #下面的方式每次在内存当中只存在一个数值: This approach is recommended for m2 = HASHLIB.MD5 () with open (' Db.txt ', encoding= ' utf-8 ', mode= ' R ') as fr:    For line in fr:        m2.update (Line.encode (' Utf-8 '))    print (M2.hexdigest ())

Operation Result:

3232ea30e0a71e5f5c3e83676f64c9eb****************3232ea30e0a71e5f5c3e83676f64c9ebprocess finished with exit code 0

Example Program 5: Get the flag ID

Import Hashlibimport timedef get_id ():    h = hashlib.md5 ()    h.update (str (Time.clock ()). Encode (' Utf-8 '    ) return H.hexdigest () print (get_id ()) print (get_id ()) print (get_id ()) print (get_id ())

Operation Result:

85b8a467159e14ec4b2d16bff39ed1995b0526b768b41eb7b4d1a8e741b1dae542999cb93fd523a787960fec38530026ce1322698e3badb958538b110 0c8d5507b7bc63304b72fa74f81d01e8317c66dprocess finished with exit code 0

  

Python Development Skills required (4)

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.