Python Learning Paste

Source: Internet
Author: User

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

  1. >>> Import Re
  2. >>> Re.search (' [ABC] ', ' Mark ')
  3. <_sre. Sre_match object at 0x001c1fa8>
  4. >>> re.sub (' [ABC] ', ' o ', ' Mark ')
  5. ' Mork '
  6. >>> re.sub (' [ABC] ', ' o ', ' Rock ')
  7. ' Rook '
  8. >>> re.sub (' [ABC] ', ' o ', ' caps ')
  9. ' 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

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.