python--module, Regular

Source: Internet
Author: User

1. module

Import module is the essence of the imported module code, from the top to the next, to find the order of the module is, first find the current directory, and then to the environment variables to find;

Modules are divided into standard modules, third-party modules, and self-written modules. The installation method is as Follows:

Standard modules

Python comes with no installation, direct import

self-written modules

Write yourself a python

Third-party Modules

Others develop good features that need to be installed

Installation mode 1:

Automatic installation

    Pip Install xxx #安装xxx模块

 Pip List #查看已安装的模块

Pip Uninstall XXX #卸载xxx模块

Pip module, Self-python3 in the python2, manual installation required

Another: Easy_install XXX can also be installed directly

Installation mode 2:

Manual Installation

Download installation package, unzip, Enter directory

Cmd run install command python XXX install

Eg:python setup.py Install

2.OS module

1        Print(OS.GETCWD ())#fetch Current Working directory2         Print(os.chdir (r"E:\byz_code\day2"))#change the current directory3         Print(os.mkdir ("test1"))#Create a folder4         Print(os.makedirs (r"Test1\test2"))#create A parent directory recursively when a folder is created and the parent directory does not exist5         Print(os.removedirs (r"Test1\test2"))#recursively delete empty directories6         Print(os.rmdir ("test1"))#delete the specified folder and delete only the empty folder7         Print(os.remove (r"E:\byz_code\day4\a.txt"))#Deleting Files8Os.rename ("Test","test1")# renaming9         Print(os.sep)#path delimiter for current operating systemTen         Print(__file__)#represents the current file one         Print(os.path.abspath ('bb.py'))#Get absolute path a         Print(os.path.dirname ())#Get Parent Directory -         Print(os.path.exists ("hhaaa"))#whether the Directory/file exists -         Print(os.path.isfile ("bb.py"))#determine if it is a file the         Print(os.path.isdir ("/usr/local"))#whether it is a path -         Print(os.path.join ("Root",'hehe','haha','A.log'))#Path Stitching

3. Time Module

1 time.sleep (1)    #sleep1s2print(int (time.time ())) #  Take the current timestamp 3print(time.strftime ("%y%m%d%h%m%s")) #  Convert a time tuple to a formatted output string              

4. SYS Module

1 sys.argv            # command line arguments list, The first element is the program itself path 2 sys.exit ('xxxxx ')  # exit the program, exit normally (0) 3 sys.path    # Returns the search path of the module, using the value of the PYTHONPATH environment variable when initializing 4#  Returns the operating system platform name
If __name__== ' __main__ ' #为当前调试代码, only executed in this document
            

5. Regular Expressions

Used to find strings, to find more complex strings, and import re for importing regular expressions.

1Re.search (r' is', s). Group ()#matches the string, finds the entire content, and returns the first one if found2Re.findall (r' is', S)#whole content lookup, return all values, form a list3 Print(re.sub (r' best',' best', S))#Replace string, receive 3 parameters,4 the first one is the regular expression, the second one is to be replaced by what,5 the third is the string to look for6 returns the original string if it is not matched7Re.split (' is', S)#Delimited String

Common Expression Symbols:

Quantity words

1* match the preceding characters 0 or more times, just *one of the preceding characters2+ Match the previous character 1 or more times, just +preceding one character3         ? Match the previous character 1 or 0 times, just one of the preceding characters4 {m} matches the previous character m times5{m,n} matches the previous character m-N Times6. Match except/characters other than n7 \ translator, for example, to find special characters, you need to transfer8| Match |left or right character9^A third parameter, re, is required to match the start of the character, and to match the beginning of each line in a multiline case. MTen                Print(re.findall (r'^b','Goodb is bes\nbesttt is HHA', Re. M))

Boundary matching

1^A third parameter, re, is required to match the start of the character, and to match the beginning of each line in a multiline case. M2                 Print(re.findall (r'^b','Goodb is bes\nbesttt is HHA', Re. M))3 $ matches the end of any character, multiple rows match the end of each line, and a third parameter, re, is Specified. M4                Print(re.findall (r'd$','besttest is good\nbest good', Re. M))5 \a starts with only characters and cannot be used for multiple lines
Print(re. FindAll(r' \ab ',' besttest is good ')) 6\z ends with only characters and cannot be used for multiple lines
            Print(re. FindAll(r' d\z ',' besttest is good '))

Pre-defined characters

1 \d Match all numbers2 \d matches Non-numeric3 \w matches numbers, letters, Chinese4 \w Match Special characters5 \s matches white space characters, such as \ t \ n \ r spaces6 \s matches non-whitespace characters
7 [xxxx] character set, matching any one of the characters in the set8Az] uppercase and lowercase letters9[a-z] Lowercase LettersTenAZ] Uppercase Letters one[0-9] All numbers aIn [] if you use ^ to represent the inverse, which is not included in these strings

Group Matching

Write some rules in a group and match the group directly, such as the IP address rule

1 Print (re.findall (r'\d{1,3} (?: \. \d{1,3}) {3}','192.168.1.3'))2TheFindAll method has a grouping, The default result is only the content within the group 3             "?:" enable "do not capture mode"              

python--module, Regular

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.