Go deep into Python

Source: Internet
Author: User

Deep Python (dive into Python) http://woodpecker.org.cn/diveintopython/toc/index.html

1 ~ Chapter 6 describes most of the content in the concise Python tutorial.

Not introduced:

Private function. All functions whose names start with two underscores (_) are similar functions.

Original string: if R is added before the string, \ In the string does not need to be written as \. For example, '\ B' can be written as R' \ B '. The regular expression must use the original string, otherwise the expression will be difficult to read.

Chapter 1 Regular Expressions

Import re

\ B character boundary '\ broad \ B' indicates to contain a separate WORD

$ '\ Broad $' At the end of the string indicates the word at the end of the sentence.

^ String start

For example, replace 'road' with 'RD .',

S = '000000'Broad road apt.3 '; Re. sub (r'broad \ B', 'RD. ', S)

Result: 100Broad RD. apt.3'

? It indicates that the character appears 0 or 1 time. Such as 'm? M? M? $ 'Can match '', 'M', 'mm', or 'mmm '. Re. Search ('m? M? M? $ ', 'Mmm ')

+ After the character indicates that the character appears once or multiple times.

{} Number of occurrences of Defined Characters: m? M? M? $ 'Can be written as' m {} $'

| Representation or example: 'A | B'

Example: confirm the roman numeralsPattern ='^ M {0, 3} (CM | cd | D? C {0, 3}) (XC | XL | L? X {0, 3}) (ix | IV | V? I {0, 3}) $'

\ D any single number

\ D any non-numeric characters

Python's default regular expressions are compact and are not easy to read. You can write the following relaxed Regular Expressions:

Pattern =""

    ^ # beginning of stringm {0, 3} # thousands-0 to 3 m's (CM | cd | D? C {900}) # Hundreds-400 (CM), 300 (CD), 0-500 (0 to 3 C's), # or 800-(d, followed by 0 to 3 C's) (XC | XL | L? X {0, 3}) # tens-90 (XC), 40 (XL), 0-30 (0 to 3 X's), # Or 50-80 (L, followed by 0 to 3 X's) (ix | IV | V? I {0, 3}) # ones-9 (IX), 4 (iv), 0-3 (0 to 3 I's), # or 5-8 (V, followed by 0 to 3 I's) $ # End of string " 

When using the relaxed type, you must add another parameter, as shown in figureRe. Search (pattern,'Mmmdccclxxx8', Re. verbose)

Example: Resolve the phone number

<Span style = "color: #222222; font-family: 'book antiqua', Georgia, palatino, times, 'times new Roman ', Serif; line-Height: 23px; font-size: Medium; "> <TT class =" prompt "> & gt; </tt> <SPAN class = "userinput"> phonepattern = Re. compile (r <SPAN class = "pystring" style = "background-color: White; color: olive;"> ''' # don't match beginning of string, number can start anywhere (\ D {3}) # area code is 3 digits (e.g. '000000') \ D * # optional separator is any number of non-digits (\ D {3}) # trunk is 3 digits (e.g. '000000') \ D * # optional separator (\ D {4}) # Rest of number is 4 digits (e.g. '000000') \ D * # optional separator (\ D *) # extension is optional and can be any number of digits $ # End of string ''' </span>, re. verbose) </span>
   & gt; & gt;   phonepattern. search ( 'work 1-(800) 555.1212 #1234 ' ). groups ()      ('000000', '000000', '000000', '000000')  

Note: brackets in (x) indicate a remembered group ). You can use groups () to obtain its value only by adding parentheses.

Chapter 2HtmlProcessing (parsing HTML files and capturing data)

Use urllib to download HTML content, and use sgmllib (sgmlparser) to analyze HTML files

Note that from module import and import module are different.

The import module retains the module namespace. Use the module name to access internal functions or attributes.

From module import imports the functions and attributes in the module into its own namespace, which can be directly used without adding the module name.

From XML. Dom import minidom XML is a package, that is, a directory that contains the special file _ init _. py.

Use dictionary to format the string '% (key) s'

Parse XML

Use the elementtree of the python standard library

XML. etree. elementtree as etree

Tree = etree. parse ('aaa. xml ')

Root = tree. getroot ()

Root. Tag

For child in root:

Unicode

In this article, network programming seems to be old and give up.

Python, learning notes
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.