This article mainly records and summarizes I read the "Python standard Library" a book, the text of this chapter of learning and understanding.
In fact, in Python, some of the ways to use text are particularly common. In general, a class such as String would be used as the most basic standard class in Python.
1.1 Functions
Capwords () and Maketrans () in the String class.
The function of Capwords () is to capitalize the first letter of all the words in a string;
The Maketrans () function creates a conversion table that can be modified by the translate () method to another set of characters, which is more efficient than repeatedly calling replace ().
There is a function called template in the string. The same is used to make the stitching of the characters.
Advanced templates can modify a string. The default syntax for template, which needs to be adjusted to find the regular expression used in the template for variable names.
############################################################# #test about Matetrans () Leet = String.maketrans (' ASDFGHJK ', ' 12345678 ') print s.translate (leet) Print s############################################################# #test about Template () values = {' var ': ' foo '}t=string. Template ("" "Variable : $varEscape : $ $Variable in text: ${var}iable" ") print ' TEMPLATE: ', T.substitute (values ) s= "" "Variable :% (Var) ssescape :%%variable in text:% (Var) sssssiable" "Print ' interpolation: ', s%values
1.2 TextWrap ()--formatted text paragraph
Function: Formats text by adjusting the position of the newline character where it appears in the paragraph.
1.3 re-Regular Expression
Function: Use formal mode to search and modify text.
Regular expression.
1.3.1 Re in search for patterns in text.
Import reprint '-' *30#about regular Expression search () pattern = ' This ' text= ' Does this text match the pattern? ' match = RE . Search (pattern, text) S=match.start () e=match.end () print ' Dound "%s" \nin "%s" \nfrom%d to%d ("%s") '% (MATCH.RE.PA Ttern,match.string,s,e,text[s:e]) #start () and end () methods can give the corresponding index in the string.
1.3.2 Compiling regular expressions
The re contains module-level functions for working with regular expressions as text strings, and compiling these expressions is more efficient for frequently used expressions. The compile () function converts an expression string into a regexobject.
print '-' *30#about the Compile () Regexes=[re.compile (p) for P in [' This ', ' so '] ]text= ' Does this text match the P Attern? ' print ' Text:%r\n '% text for regex in regexes: print ' seeking '%s ', '% regex.pattern if Regex.searc H (text): print ' match! ' else: print ' no match! '
A module-level function maintains a cache of compiled expressions, but the size of the cache is limited, and using compiled expressions directly avoids caching lookup overhead. Another benefit of using compiled expressions is that the process of compiling is advanced in a way that optimizes the efficiency of the program during operation.
1.3.3 Multiple matches
Search () in front, is used to find a single instance of a text string. The FindAll () function returns all substrings in the input that match the pattern and do not overlap.
print '-' *30#about the FindAll () Text = ' Bbbbbababbababbabbbaba ' pattern = ' ba ' for match in Re.findall (pattern, text): Print Matchprint '-' *30#about the Finditer () #finditer会返回一个迭代器, you can generate a match instance instead of a string that is returned directly as FindAll (). text= ' Aaaadaaaaadadadada ' pattern= ' da ' for match in Re.finditer (pattern,text): s=match.start () e=match.end ( ) print ' Found '%s ' at%d:%d '% (text[s:e],s,e)
1.3.4 pattern Syntax
The pattern syntax of a python regular expression.
1.3.5 Limit Search
If you know in advance that you only need to search a subset of the entire input, you can tell the RE prophet search scope to further constrain the regular expression.
print '-' *30# is a less efficient way to implement Iterall (). Text= ' This was some text--with punctuation. ' Pattern=re.compile (R ' \b\w*is\w*\b ') print ' text: ', Textpos=0while True: Match=pattern.search (text,pos) print match if match: Break S=match.start () e= Match.end () print s,e print '%d:%d = '%s ' '% (S,e-1,text[s:e]) pos=e
Text in Python (i)