1. Python provides support for regular expressions through the RE module. The general step for using RE is to use the Re.compile () function, compile the string form of the regular expression into a pattern instance, then use the pattern instance to process the text and get the matching result (a match instance), and finally use the match instance to get the information. To do other things.
To give a simple example, look for all the English characters in a string:
Import re
Pattern = Re.compile (' [a-za-z] ')
result = Pattern.findall (' As3siopdj#@23awe ')
Print result
# [' A ', ' s ', ' s ', ' I ', ' O ', ' P ', ' d ', ' j ', ' A ', ' w ', ' e ']
2. The Re.sub function takes a regular expression-based substitution work
- >>> Import Re
- >>> Re.search (' [ABC] ', ' Mark ')
- <_sre. Sre_match object at 0x001c1fa8>
- >>> re.sub (' [ABC] ', ' o ', ' Mark ')
- ' Mork '
- >>> re.sub (' [ABC] ', ' o ', ' Rock ')
- ' Rook '
- >>> re.sub (' [ABC] ', ' o ', ' caps ')
- ' Oops '
MarkContains a , b or is c it? Yes, it contains a .
OK, now find out a , b or, c and o replace it. Markit becomes Mork .
The same method can rock become rook .
You may think that it can turn out to be caps oaps , but that is not the case. re.subReplace all occurrences, not just the first one. So the regular expression will turn caps into oops , because c and a are converted to o .
Re.sub's function prototype is: re.sub (Pattern, REPL, string, count)
Where the second function is the replaced string, in this case '-'
The fourth parameter refers to the number of replacements. The default is 0, which means that each match is replaced.
3. Re.split
You can use Re.split to split a string, such as: Re.split (R ' \s+ ', text), and divide the string into a word list by space.
Re.findall
Re.findall can get all the matching strings in the string. such as: Re.findall (R ' \w*oo\w* ', text); Gets all the words in the string that contain ' oo '.
Re.match's function prototype is: Re.match (pattern, string, flags)
The first parameter is a regular expression, here is "(\w+) \s", if the match succeeds, returns a match, otherwise returns a none;
The second parameter represents the string to match;
The third parameter is the Peugeot bit, which controls how regular expressions are matched, such as case sensitivity, multiline matching, and so on.
Import re
Text = "Jgood is a handsome boy, he's cool, clever, and so on ..."
m = Re.match (r "(\w+) \s", text)
If M:
Print M.group (0), ' \ n ', M.group (1)
Else
print ' not match '
4. Different Catalog import modules
Error message "Importerror:no module named ' Fibo '"
Workaround: Create a new empty file under the directory __init__.py
5. Python2.7 Chinese not recognized
Error message "Syntaxerror:non-ascii character ' \xc9 '"
Workaround: File Header #coding=gbk
6. With usage
With Conn:
Conn.execute ("INSERT into sometable values (?,?)", ("foo", "Bar"))
In this example, commit () is executed automatically after all the statements in the with data block have been executed without errors, and if any exceptions occur, the rollback () operation will be performed, prompting the exception again
7. Django Template Error
Error message: Requested setting template_debug, but settings is not configured. You must either define
Workaround: From django.conf import settings
Settings.configure ()
Python Learning Paste