Python 3 standard library courseware first chapter (Second Edition)

Chapter
Text
1.1 string: template text constant and
1.2 textwrap: formatted text paragraph
1.3 re: regex
1.4 difflib: comparing the sequences
str class,
string.Template
TextWrap
difflib
------------- ---------
1.1 string: text constants and template
string str module module
function capwords ()
-------------------
Jupyter note --- Cupid notebook

Chapter One
Text processing tools: str class
string.Template and easy way to build richer than the str string object properties.
Web framework defined templates or Python Package Index provides extension modules compared string.Template although not as feature-rich, but it does support a good template users can modify these templates need dynamic values ​​into static text in.
Tool module comprising textwrap paragraph text format, the width of the output can be limited, increasing indentation, so that line breaks can be inserted and consistent wrap.
1.1 string: text constants and templates
string module at the earliest versions of Python already have. Many functions formerly provided by this module has a method for the str object transplant, but the module still retains a lot of useful constants and classes to handle str object. Here we will focus on these constants and classes.
 
1.1.1 Function
    Function capwords () put a string of all words capitalized.
       Listing 1-1: string_capwords.py
       --------------------------------
       Import String
       = S 'of The Quick Brown Fox jumped over the lazy The Dog.'
       Print (S)
       Print (string.capwords (S))
       ----------------------- ---------
       result:
       at The Quick Brown Fox jumped over lazy at The Dog.
       at The Quick Brown Fox jumped over at The lazy Dog.
------------------- -----------------------
this result code is equivalent to first call the split (), the result list of capitalized words, then call join () to merge the results.
1.1.2 template
PEP 292: Simpler string replacement.
String template is PEP 292 new part, built as a mosaic syntax instead of practice. When using string.Template splicing, to $ prefix in front of the name to identify the variables (e.g., $ var). Alternatively, if necessary to distinguish between variable and the surrounding text, braces can be
surrounded by variables (e.g., $ {var}).
The following simple example of a template,% similar string concatenation operator and using str.format () syntax new format string were compared:
string.Template
       Listing 1-2: string_template.py
       ------------------------------
       Import String
       values = {'var': 'foo'}
       
       t = string.Template("""
       Variable  :$var
       Escape    :$$
       Variable in text :${var}iable
       """
       )
       
       print('TEMPLATE:', t.substitute(values))
       
       s = """
       Variable  :%(var)s
       Escape  :%%
       Variable in text :%(var)siable       
       """
       
       print('INTERPOLATION', s % values)
       
       s = """
       Variable  : %{var}
       Escape    : {{}}
       Variable in text :{var}siable
       """
       
       print('FORMAT:', s.format(**values))
       -----------------------------------
In the first two cases, the trigger character ($ or%) to repeat twice to escape. Syntax formatting, and need to be repeated} {escaped.
-----------------------------------------------
TEMPLATE:
Variable : foo
Escape: $
Variable in text: fooiable
INTERPOLATION
Variable  :foo
Escape  :%
Variable in text :fooiable       
The FORMAT:
Variable:% foo
the Escape: {}
Variable in text: foosiable
----------------------------------- --------------
template with a key difference string concatenation or format is that it does not consider the type of the parameter. Values are converted to a string, then the string into the result. There is no offer formatting options. For example, there is no way to control the use of several significant figures to represent a floating point value.
However, it also has the advantage by using sfe_substitute () method, to avoid failure to provide an exception that can occur when all of the required parameter values to the template.
-------------------------------------------------
       Code Listing 1-3: string_template_missing.py
       ---------------------------------------
       Import String
       = {values 'var': 'foo'}
       
       T = string.Template ( "$ var IS IS here Wallpaper Missing But Not Provided $")
       
       the try:
        Print ( ' `substitute ():', t.substitute (values))
       the except a KeyError ERR AS:
        Print ( 'ERROR:', STR (ERR))
        
       Print ( 'safe_substitute ():', t.safe_substitute (values))
       ------------------- --------------------
       results:
       ERROR: 'Missing'
       safe_substitute (): $ foo Missing IS IS here Wallpaper But not Provided
       ---------- -----------------------------
Since the value of missing values not in the dictionary, so the substitute () generates a KeyError. safe_substitute () is different, it will not throw this error, but the error will capture and retain variable expression in the text.
1.1.3 Advanced template
can be adjusted string.Template find regular expression pattern variable names used in the template body to change its default syntax. For this purpose, a simple method is to modify idpattern delimiter and class attributes.
       Listing 1-4: string_template_advanced.py
       -----------------------------------------
       import string
       class MyTemplate(string.Template):
        delimiter = '%'
        idpattern = '[a-z]+_[a-z]+'
        
       template_text = '''
       Delimiter : %%
       Replaced  : %with_underscore
       Ignored   : %notunderscored
       '''
       
       d = {
        'with_underscore': 'replaced',
        'notunderscored':  'not replaced',
       }
       
       t = MyTemplate(template_text)
       print('Modified ID pattern: ')
       print(t.safe_substitute(d))
       ------------------------------------------
       Modified ID pattern:
       DELIMITER:%
       Replaced: REPLACED
       Ignored:% notunderscored
       ---------------------------------------- -
in this case, replace the rules have changed, rather than the $ delimiter is%, and somewhere in the middle of a variable name must contain an underscore. Mode% notunderscored not be replaced by any string, because it does not contain the underscore character.
To complete the more complex changes, you can cover the pattern attribute and define a new regular expression. Provided by the model must contain four named groups, respectively capture escaped delimiter, named variables, variable names in parentheses and illegal delimiter mode.
  

1.2 textwrap: formatted text paragraphs
fill () function takes as input text, formatted text generated as output.
1.2.3 remove the existing indentation
DeDent ()
docstring
dedent () to remove the indent
indent () indent
_Line_one.
___Line_one.
_Line_three.
 
1.2.4 combined dedent and fill
1.2.5 indented block
indent ()
of the first line of text, indent.
>
1.2.7 truncate long text

1.3 re: Regular Expressions
regex or regexp
和sed awk grep
Perl Ruby Tcl Awk
C C ++ Python
1.3.1 Find text mode
compile () function
RegexObject
1.3.3 multiple matches
findall () function
1.3.4.1 repeated
{m, n-}
{m,}
------------------------
busy day, disaster recovery environment, our unit, system connectivity testing to do,
the atmosphere to be relaxed, the language to be cheerful a little, to feel happier,
------------------------
learning, growing process,
------------- -----------
1.3.4.1 repeat
yuan characters
*: ?
m} {
{m, n-}
(m {,})
greedy expression of the form (a [ab] +)
1.3.4.3 escape code
     Table 1-1 escape code expressions
  escape code meaning
  \ d digital
  \ D non-digital
  \ s white space (tabs, spaces, line feed)
  \ S non-whitespace character
  \ w alphanumeric
  \ W non-alphanumeric
- ----------------------------------------
1.3.4.4 anchored
----- ----------------
 table 1-2 regular expression code anchor
 anchor symbol meaning
 the beginning of the line or string ^
 $ end of the line or string
 \ a string beginning
 \ Z end of the string
 \ b empty string end or beginning of a word
 \ B word is not null string at the beginning or end
----------------------------
1.3.5 limit the search
----------------------------
match.groups () according to the order in the set of matched string expression returns a string sequence.
----------------------------
1.3.7 Search Options
----------------------------
You can use bits or (OR)
the compile (), Search (), match ( )
----------------------------
literal IGNORECASE mode makes the characters and character ranges with uppercase and lowercase characters are matched.
----------------------------
 embedded flags may be used in combination in the same group.
      
      Table 1-3 Regular expressions abbreviation
------------------------------------------ ---------------------------------
   flag Acronym
   the ASCII A
   the IGNORECASE I
   MULTILINE m
   the DOTALL S
   the VERBOSE X
----- -------------------------------------------------- ----------------------
before 1.3.8 or backward
-------------------- -------------------------------------------------- -------
   Table 1-4 difflib.get_opcodes () instruction
   --------------------------------
   opcode defined
   'replace' to a [i1: i2] is replaced with b [j1: j2]
   'delete' completely removed A [i1: i2]
   'iNSERT' will b [j1: j2] insert A [i1: i2]
   'equal' sequence has two equal
- -------------------------------------------------

Guess you like

Origin www.cnblogs.com/niaocaizhou/p/11714599.html