06_ Module (ii) _ Regular

Source: Internet
Author: User

1 sys

Progress bar Instances

Import sysimport timedef view_bar (num, total): rate    = float (num)/float (total)    rate_num = Int (rate *)    r = ' \r%d%% '% (rate_num,) # \ r Restart from start position    Sys.stdout.write (R)  # do not wrap output    Sys.stdout.flush ()  # empty output if __name __ = = ' __main__ ':    for I in range (0, +):        time.sleep (0.1)        View_bar (i, 100)
1.2 OS
OS.GETCWD () Gets the current working directory, that is, the directory path of the current Python script work os.chdir ("DirName") changes the current script working directory, equivalent to the shell Cdos.curdir Return to current directory: ('. ') Os.pardir Gets the parent directory string name of the current directory: (' ... ') Os.makedirs (' Dir1/dir2 ') can generate a multi-level recursive directory Os.removedirs (' dirname1 ') if the directory is empty, then deleted, and recursively to the previous level of the directory, if also empty, then delete, and so on Os.mkdir (' DirName ' Generate a single-level directory, equivalent to the shell mkdir dirnameos.rmdir (' dirname ') delete the single-level empty directory, if the directory is not empty can not be deleted, error; equivalent to RmDir dirnameos.listdir in the shell (' dirname ') lists all files and subdirectories under the specified directory, including hidden files, and prints os.remove () Delete a file Os.rename ("Oldname", "new") Rename File/directory OS                  . Stat (' Path/filename ') gets file/directory information OS.SEP operating system-specific path delimiter, win under "\ \", Linux for "/" Os.linesep The current platform uses the line terminator, win under "\t\n", Linux for "\ n" os.pathsep the string used to split the file path Os.name string indicates the current use of the platform. Win-> ' NT '; Linux-> ' POSIX ' Os.system ("Bash command") runs a shell command that directly displays Os.environ get system environment variablesOs.path.abspath (path) returns the absolute path normalized by pathOs.path.split (path) splits path into directory and file name two tuples returnedos.path.dirname (path) returns the directory of path. is actually the first element of Os.path.split (path)Os.path.basename (Path) returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path), os.path.exists (path), returns True if path exists, or Falseos.path.isabs if path is not present if path is An absolute path that returns Trueos.path.isfile (path) If path is an existing file that returns TRUE. Otherwise, return Falseos.path.isdir (path) True if path is a directory that exists. otherwise returns falseOs.path.join (path1[, path2[, ...]) When multiple paths are combined, the parameters before the first absolute path are ignoredOs.path.getatime (Path) returns the last Access time Os.path.getmtime (path) of the file or directory to which path is pointing returns the last modified time of the file or directory to which path is pointing

Note: Yellow selection must be known

1.3 Hashlib

For cryptographic related operations, instead of the MD5 module and the SHA module, mainly provides SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm

1.3.1 MD5
Import hashlib# add specific characters when executing MD5, encrypt again on original encryption, commit security, prevent collision with library obj = hashlib.md5 (bytes (' ABCDE ', encoding= "Utf-8") # Gets the cryptographic result obj.update (bytes (' 123456 ', encoding= ' utf-8 ')) result = Obj.hexdigest () print (Result) # Output result 985ae3acc7684c692870900af17ab160
1.4 Re-regular expression

Character:

. Match any character other than line break

\w match letters or numbers or underscores or kanji

\s matches any whitespace character

\d Matching numbers

\b Match the beginning or end of a word

^ Start of matching string

$ match End of string

Number:

* Repeat 0 or more times

+ Repeat one or more times

? Repeat 0 or one time

{n} repeats n times

{n,} repeats n or more times

{N,m} repeats n to M times

1.4.1 Re.findall character Matching

FindAll, gets a non-repeating match list, and if one group is returned as a list, each match is a string, and if there are multiple groups in the model, it is returned as a list, and each match is Ganso, and an empty match is included in the result.

FindAll (Pattern, string, flags=0)

1. Ordinary characters

ret = Re.findall (' Alex ', ' Yuanalesxalexwupeiqi ') print ("Normal character", ret)

2. Meta-characters

. Any one character
^ Start position Matching
$ End Position Match
* Match previous character 0 to multiple
+ Repeat one or more times
? Repeat 0 or one time
{} Repeat N times
[] or (^ in [] is not)

Practice:

ret = Re.findall (' al.x ', ' Yuanalesxalexwupeiqi ') print (".", ret) ret = Re.findall (' ^al.x ', ' Alexyuanalesxalexwupeiqi ') Print ("^", ret) ret = Re.findall (' pei.i$ ', ' Yuanalesxalexwupeiqi ') print ("$", ret) ret = Re.findall (' al.*x ', ' Yuanalesxalexwupeiqi ') Print ("*", ret) ret = Re.findall (' al.+x ', ' Yuanalesxalexwupeiqi ') print ("+", ret) ret = Re.findall (' al.? X ', ' Yuanalesxalexwupeiqi ') print ("?", ret) ret = Re.findall (' al.{1,6}x ', ' Yuanalesxalexwupeiqi ') of print ("{}", ret) ret = Re.findall (' a[a-z]+x ', ' Yuanalesxalexwupeiqi ') print ("[]:", ret) # outputs the result. [' Alex ']^ [' Alex ']$ [' Peiqi ']* [' Alesxalex ']+ [' Alesxalex ']? [' Alex '] {} [' Alesxalex '] []: [' Alex ']
1.4.2 Re.match

Match, which matches from the starting position, matches the successful return of an object, and the match returns none successfully.

Match (pattern, string, flags=0) # pattern: Regular Model # string:  string to match # Flags:   compile flag bits to modify how regular expressions are matched, such as: case-sensitive, Multi-line matching, etc.

Practice:

ret = re.match (' com ', ' Comwww.runcomoob '). Group () print (ret) ret = re.match (' com ', ' Comwww.runcomoob ', re. I). Group () print (ret) # output result comcom
1.4.3 Re.search

Search, browse the entire string to match the first one, the unmatched successful return none

Search (pattern, string, flags=0) group () returns the string that is matched by re () Start () returns the position where the match  was    started stop ()      returns the position of the match end span ()    Returns a tuple containing the location of a match (start, end)

Practice:

A = "123abc456" ret = Re.search ("([0-9]*) ([a-z]*) ([0-9]*)", a) print (Ret.group (0)) print (Ret.group (1)) Print (Ret.group ( 2)) Print (Ret.group (3)) # Output result 123abc456123abc456

  

1.4.4 Re.finditer

Finditer, returns an iterator

Finditer (Pattern, string, flags=0)
1.4.5 Re.sub

Sub, replacing the specified location string that matches the success

Sub (pattern, REPL, String, count=0, flags=0) # pattern: Regular Model # REPL   : string to replace or executable # String: string to Match # count  : Specifies a match Number # Flags  : Matching pattern

Practice:

ret = re.sub ("g.t", "has", ' I get a, I got B, I gut C ')    #全部更换print (ret) Ret1 = re.sub ("g.t", "have", ' I get a, I got B, I Gut C ', 2)   #只替换2次print (ret1) # Output I have a, I has B, I have C  i has a, I have B, I gut C  
1.4.6 RE.SUBN

Using the same re.sub, the return value is displayed with a replacement several times

ret = RE.SUBN ("g.t", "have", ' I get a, I got B, I gut C ') print (ret) Ret1 = re.subn ("g.t", "has", ' I get a, I got B, I gut C ', 2 Print (RET1) # output result (' I have A, I has B, I have C ', 3) (' I has a, I have B, I gut C ', 2)
1.4.7 Re.split

Split, split string according to regular match

Split (pattern, String, maxsplit=0, flags=0) # pattern: Regular Model # string: String to match # Maxsplit: Specify number of Splits # Flags  : Match pattern

Practice:

ret = re.split (' \d+ ', ' One1two2three3four4 ') print (ret) ret = re.split (' \d+ ', ' One1two2three3four ') print (ret) # output result [' One ', ' one ', ' three ', ' four ', ']   #最后为空, because the numbers are divided, the number 4 is empty [' one ', ' one ', ' one ', ' three ', ' four ']
1.4.8 Re.compile

Re.compile that is used to compile a regular expression in the form of a string as a pattern object.

Increase the reusability of code and improve efficiency.

Compile (pattern, flags=0)

Practice:

Text1 = "Jgood is a handsome boy, he's cool, clever, and so on ..." text2 = "good night, Good morning" regex = Re.compile (r ' \w*oo\w* ')  # Compile Expression object print (Regex.findall (Text1))  # operation on string 1 print (Regex.findall (text2))  # for String 2 operation # output result [ ' Jgood ', ' cool ' [' good ', ' good ']
1.4.9 Regular grouping

Purpose: To extract data from the data that has been matched

Origin = "Hello Alex bcd Alex Lge Alex ACD" r1 = Re.split ("(Alex)", origin, 1)  #以alex为分割符分割字符串, split once, and output the delimiter alexprint ( R1) r2 = Re.split ("(Al (ex))", origin, 1)  #以alex为分割符分割字符串, and re-infringed with ex, and output delimiter Alexprint (R2) # output [' Hello ', ' Alex ', ' BCD Alex Lge Alex ACD ' [' Hello ', ' Alex ', ' Ex ', ' BCD Alex Lge Alex ACD 19 ']

06_ Module (ii) _ 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.