The common module of Python career (ii)

Source: Internet
Author: User
Tags hmac sha1

JSON & Pickle Modules

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

Shelve module

The shelve module is a simple k,v module that persists memory data through a file and can persist any Python data format that pickle can support

Import shelved = Shelve.open (' shelve_test ') #打开一个文件 class Test (object):    def __init__ (self,n):        SELF.N = NT = Test ( 123)  t2 = Test (123334) name = ["Alex", "Rain", "Test"] d["test"] = name #持久化列表d ["T1"] = t      #持久化类d ["t2"] = T2d.close ()
XML Processing Module

XML is a protocol that implements data exchange between different languages or programs, much like JSON, but JSON is simpler to use, but in ancient times, in the Dark Ages when JSON was not yet born, you could only choose to use XML, and so far many traditional companies, such as the financial industry, are mostly XML-like interfaces.

The format of XML is as follows: by the <> node to distinguish the data structure.

1234567891011121314151617181920212223 <?xmlversion="1.0"?><data>    <countryname="Liechtenstein">        <rankupdated="yes">2</rank>        <year>2008</year>        <gdppc>141100</gdppc>        <neighborname="Austria" direction="E"/>        <neighborname="Switzerland" direction="W"/>    </country>    <country name="Singapore">        <rankupdated="yes">5</rank>        <year>2011</year>        <gdppc>59900</gdppc>        <neighborname="Malaysia" direction="N"/>    </country>    <countryname="Panama">        <rankupdated="yes">69</rank>        <year>2011</year>        <gdppc>13600</gdppc>        <neighborname="Costa Rica" direction="W"/>        <neighborname="Colombia" direction="E"/>    </country></data>

XML protocols are supported in each language, and in Python you can manipulate XML with the following modules

Import Xml.etree.ElementTree as Ettree = Et.parse ("xmltest.xml") root = Tree.getroot () print (Root.tag) #遍历xml文档for Child In Root:    print (Child.tag, child.attrib) for I in child    :        print (i.tag,i.text) #只遍历year nodes for node in Root.iter (' Year '):    print (Node.tag,node.text)

modifying and deleting XML document content

Import Xml.etree.ElementTree as Ettree = Et.parse ("xmltest.xml") root = Tree.getroot () #修改for node in Root.iter (' year '): c0/>new_year = Int (node.text) + 1    node.text = str (new_year)    Node.set ("updated", "yes") tree.write ("Xmltest.xml ") #删除nodefor Country in Root.findall (' Country '):   rank = int (country.find (' rank '). Text)   If rank >:     Root.remove (country) tree.write (' Output.xml ')

Create your own XML document

Configparser Module

Used to generate and modify common configuration documents, the name of the current module is changed to Configparser in the Python 3.x version.

Here's a look at a lot of the common document formats for software

[DEFAULT] Serveraliveinterval = 45Compression = Yescompressionlevel = 9forwardx11 = Yes[bitbucket.org]user = hg[ Topsecret.server.com]port = 50022forwardx11 = no

What if you want to use Python to generate a document like this?

Import Configparserconfig = Configparser. Configparser () config["DEFAULT"] = {' Serveraliveinterval ': ' A ',                      ' Compression ': ' Yes ',                     ' compressionlevel ': ' 9 '}config[' bitbucket.org ' = {}config[' bitbucket.org ' [' User '] = ' HG ' config[' topsecret.server.com '] = {}topsecret = config[' topsecret.server.com ']topsecret[' Host Port ' = ' 50022 '     # mutates the parsertopsecret[' ForwardX11 '] = ' no ' c3/># same hereconfig[' DEFAULT ' [' ForwardX11 '] = ' yes ' with open (' Example.ini ', ' W ') as ConfigFile:   config.write ( ConfigFile)

  

When you're done, you can read it again.

>>> Import configparser>>> config = Configparser. Configparser () >>> config.sections () []>>> config.read (' Example.ini ') [' Example.ini ']>>> Config.sections () [' bitbucket.org ', ' topsecret.server.com ']>>> ' bitbucket.org ' in configtrue>>> ' Bytebong.com ' in configfalse>>> config[' bitbucket.org ' [' User '] ' HG ' >>> config[' DEFAULT ' [' Compression ' yes ' >>> Topsecret = config[' topsecret.server.com ']>>> topsecret[' ForwardX11 '] ' no ' >>> topsecret[' Port ' ' 50022 ' >>> for key in config[' bitbucket.org ']: print (key) ... usercompressionlevelserveraliveintervalcompressionforwardx11>>> config[' bitbucket.org ' [' ForwardX11 '] ' Yes

Configparser additions and deletions to check grammar

[SECTION1]K1 = v1k2:v2 [section2]k1 = v1import configparser config = configparser.configparser () config.read (' I.cfg ') # # # # ######## Read ########## #secs = config.sections () #print secs#options = config.options (' group2 ') #print options #item_list = C Onfig.items (' group2 ') #print item_list #val = config.get (' group1 ', ' key ') #val = Config.getint (' group1 ', ' key ') # ######## # # rewrite ########## #sec = config.remove_section (' group1 ') #config. Write (Open (' I.cfg ', "w")) #sec = Config.has_section (' Wupeiqi ') #sec = config.add_section (' Wupeiqi ') #config. Write (Open (' I.cfg ', "w"))  #config. Set (' group2 ', ' K1 ', 11111) #config. Write (Open (' I.cfg ', "w") #config. Remove_option (' group2 ', ' age ') #config. Write (' I.cfg ', "w")
hashlib Module

For cryptographic related operations, the 3.x replaces the MD5 module and the SHA module, mainly providing SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm

Import HASHLIBM = Hashlib.md5 () m.update (b "Hello") M.update (b "It ' s Me") print (M.digest ()) m.update (b "It ' s been a long time Since last time we ... ") Print (M.digest ()) #2进制格式hashprint (Len (M.hexdigest ())) #16进制格式hash" Def Digest (self, *args, * *    Kwargs): # Real signature Unknown "" "Return the Digest value as a string of binary data." " Passdef hexdigest (self, *args, **kwargs): # Real signature Unknown "" "Return the Digest value as a string of HEXADECIM Al digits. "" "Pass" "Import hashlib# ######## md5 ####### #hash = HASHLIB.MD5 () hash.update (' admin ') print (Hash.hexdigest ()) # ##### # # # SHA1 ####### #hash = HASHLIB.SHA1 () hash.update (' admin ') print (Hash.hexdigest ()) # ######## sha256 ####### #hash = hashlib.sha256 () hash.update (' admin ') print (Hash.hexdigest ()) # ######## sha384 ####### #hash = hashlib.sha384 () Hash.update (' admin ') print (Hash.hexdigest ()) # ######## sha512 ####### #hash = hashlib.sha512 () hash.update (' admin ') Print (Hash.hexdigest ())

Not enough to hang? Python also has an HMAC module that internally creates keys and content for us to process and then encrypt

Hash message authentication code, or HMAC, is an authentication mechanism based on the message identification Code (authentication code) of Mac. When using HMAC, both sides of the message communication authenticate the authenticity of the message by verifying the authentication key K added in the message;

Generally used in network communication message encryption, the premise is that both parties must first agree on a good key, like a connector password, and then send the message with key to encrypt the message, the receiver with key + message plaintext again encrypted, take the encrypted value with the sender's relative ratio is equal, so that the authenticity of the message can be verified, And the legitimacy of the sender.

Import Hmach = hmac.new (b ' King cover to Tiger ', B ' Pagoda Town River Demon ') print h.hexdigest ()

Read more about md5,sha1,sha256 and other introductory articles here https://www.tbs-certificates.co.uk/FAQ/en/sha256.html

Re module

Common Regular Expression symbols

‘.‘ The default match is any character except \ n, if you specify flag Dotall, match any character, including the newline ' ^ ' match character beginning, and if you specify flags MULTILINE, this can also match (r "^a", "\nabc\neee", Flags=re. MULTILINE) ' $ ' matches the end of the character, or E.search ("foo$", "BFOO\NSDFSF", Flags=re. MULTILINE). Group () can also ' * ' match the character before the * number 0 or more times, Re.findall ("ab*", "CABB3ABCBBAC") result for [' ABB ', ' ab ', ' a '] ' + ' matches the previous character 1 or more times, Re.findall ("ab+", "Ab+cd+abb+bba") results [' AB ', ' ABB '] '? ' Match previous character 1 times or 0 times ' {m} ' match previous character M times ' {n,m} ' matches previous character N to M times, Re.findall ("ab{1,3}", "ABB ABC abbcbbb") Results ' ABB ', ' AB ', ' ABB '] ' | ' Match | left or | Right character, re.search ("abc| ABC "," ABCBABCCD "). Group () Results ' ABC ' (...) ' Group match, Re.search (" (ABC) {2}a (123|456) C "," abcabca456c "). Group () results abcabca456c ' \a ' matches only from the beginning of the character, Re.search ("\aabc", "ALEXABC") is not matched to the ' \z ' match character end, the same as $ ' \d ' matches the number 0-9 ' \d ' matches the non-numeric ' \w ' match [a-za-z0-9] ' \ W ' matches non-[a-za-z0-9] ' s ' matching whitespace characters, \ t, \ n, \ r, Re.search ("\s+", "Ab\tc1\n3"). Group () result ' \ t ' (? P<name&gt, ...) ' Group Matching Re.search (? P<province>[0-9]{4}) (? P<city>[0-9]{2}) (? P&LT;BIRTHDAY&GT;[0-9]{4}) "," 371481199306143242 "). Groupdict (" city ") result {' Province ': ' 3714 ', ' City ': ' Bayi ', ' birthday ' : ' 1993 '}

  

The most commonly used match syntax

The haunting of the backslash
As with most programming languages, "\" is used as an escape character in regular expressions, which can cause a backslash to be plagued. If you need to match the character "\" in the text, then 4 backslashes "\\\\" will be required in the regular expression expressed in the programming language: the first two and the last two are used to escape the backslash in the programming language, converted to two backslashes, and then escaped in the regular expression into a backslash. The native string in Python solves this problem well, and the regular expression in this example can be expressed using R "\ \". Similarly, a "\\d" that matches a number can be written as r "\d". With the native string, you no longer have to worry about missing the backslash, and the expression is more intuitive.

Only a few matching patterns to be known lightly

Re. I (re. IGNORECASE): Ignoring case (full notation in parentheses, same as below) M (MULTILINE): Multiline mode, change the behavior of ' ^ ' and ' $ ' (see) S (dotall): Point any match pattern, change '. ' The behavior

The common module of Python career (ii)

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.