Some usage of Python is Shared
- 2020-04-02 09:36:07
- OfStack
1) the use of regular expressions.
2) use global variables in functions.
3)python default print output newline.
If you need to output without breaking a line, add a comma at the end.
4) sharding of strings.
Split () is used according to a string, and the default parameter is whitespace, including Spaces, carriage returns, tabs, etc. :
StrList = STRS. Split (' _ ')
If you need to shred by multiple strings, you can use regular expressions:
5) determine if a string is a number.
6) read and write files
7) use of lists
# Regular expression modules
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.
def func():
global num
3)python default print output newline.
If you need to output without breaking a line, add a comma at the end.
print 'Hello World!',
4) sharding of strings.
Split () is used according to a string, and the default parameter is whitespace, including Spaces, carriage returns, tabs, etc. :
StrList = STRS. Split (' _ ')
If you need to shred by multiple strings, you can use regular expressions:
# Shards by Spaces and horizontal tabs
strList = re.split("[ts]", strs)
5) determine if a string is a number.
if str.isdigit():
return True
else:
return False
6) read and write files
# Read the file
fin = file('1.txt', 'r')
# Write files
fout = file('1_ans.txt', 'w')
while True:
line = fin.readline()
# At the end of file
if len(line)==0:
break
fout.write(line)
fin.close()
fout.close()
7) use of lists
ansList = []
# Increases the value in the list
ansList.append('Hello1')
ansList.append('Hello2')
# Sort the list
ansList.sort()
# Traverse the output
for ans in ansList
print ans