I learned some python recently. I had to process a file yesterday, so I tried it.
1) use of regular expressions.
The code is as follows:
# Regular expression module
Import re
# Regular expression
RePattern = '. * [0-9] {4 }'
Pattern = re. compile (rePattern)
# Matching
If pattern. match (line ):
Return True
Else:
Return False
2) use global variables in functions.
The code is as follows:
Def func ():
Global num
3) by default, python outputs a line feed in print.
If you do not need to output a line break, add a comma at the end.
The code is as follows:
Print 'Hello World! ',
4) string splitting.
Use split () to split a string. the default parameter is blank, including spaces, carriage returns, and tabs:
StrList = strs. split ('_')
To split strings, use the regular expression:
The code is as follows:
# Split based on spaces and horizontal tabs
StrList = re. split ("[\ t \ s]", strs)
5) determine whether a string is a number.
The code is as follows:
If str. isdigit ():
Return True
Else:
Return False
6) file read/write
The code is as follows:
# Reading files
Fin = file('1.txt ', 'r ')
# Writing files
Fout = file('audio ans.txt ', 'w ')
While True:
Line = fin. readline ()
# End of a file
If len (line) = 0:
Break
Fout. write (line)
Fin. close ()
Fout. close ()
7) Use of the list
The code is as follows:
AnsList = []
# Adding values in the list
AnsList. append ('hello1 ')
AnsList. append ('hello2 ')
# Sort the list
AnsList. sort ()
# Traverse output
For ans in ansList
Print ans