python常用腳本模板:
- 數組
- 正則表達式re.split/re.search/re.sub
- 調用shell命令
- 讀寫文本
#!/usr/bin/env python
# readme
# 1. print need () in python 3.x
# 2. recommend re.split, no string.split
# 3. print will add "'n" default. """print(line,end='')""" in python 3.x; """print line,""" in python 2.x .
import os;
import re;
import sys; # for debug
# list var
list=["nihao","123",456,"hello"]
list[1]='word'
for i in list:
print(i)
# string replace
list[1]=list[1].replace("word","world")
for i in list:
print(i)
# recommend re.split, no string.split
str="hello world \n welcome"
print(str.split(' '))
print(str.split('\n'))
# re.split
str="hello world \n welcome"
print(re.split(r'\s+',str))
print(re.split(r' |\n',str))
print(re.split(r'\n+',str))
print(re.split(r'\w+',str))
# shell command in common
os.system(r'rm -f out.log')
# shell command should return value
P=os.popen('ls -al','r')
output=P.read()
print(output)
P.close
# for debug
print(sys._getframe().f_lineno)
os.system(r'rm -f out.log')
# re.search()
# re.sub()
# like perl process file with regexp
# read file from setup.py
# write file to out.log
F=open("setup.py","r")
F1=open("out.log","w")
for line in F:
matchObj=re.search(r'(.*)run(.*)',line)
if matchObj:
print("!!!!!!!!!!!!!Match!!!!!!!!!!!",matchObj.group())
matchObj_sub=re.sub(r'(.*)run(.*)',r'\1rruunn\2',line)
F1.write(matchObj_sub)
else:
print(line,end='') # end='' can ignore "\n" from variable "line"
pass
F.close
F1.close
執行結果:
qilei@GKCSX091 MINGW32 ~/project/studyEnglish (master)
$ ./setup.py
nihao
word
456
hello
nihao
world
456
hello
['hello', 'world', '\n', 'welcome']
['hello world ', ' welcome']
['hello', 'world', 'welcome']
['hello', 'world', '', '', 'welcome']
['hello world ', ' welcome']
['', ' ', ' \n ', '']
total 1132
drwxr-xr-x 1 qilei 1049089 0 9月 16 17:52 .
drwxr-xr-x 1 qilei 1049089 0 9月 10 15:34 ..
-rw-r--r-- 1 qilei 1049089 1150390 9月 27 2018 Arete Research - AI Silicon Rage Against the Machines.pdf
-rwxr-xr-x 1 qilei 1049089 1434 9月 16 17:51 setup.py
42
#!/usr/bin/env python
# readme
# 1. print need () in python 3.x
# 2. recommend re.split, no string.split
# 3. print will add "'n" default. """print(line,end='')""" in python 3.x; """print line,""" in python 2.x .
import os;
import re;
import sys; # for debug
# list var
list=["nihao","123",456,"hello"]
list[1]='word'
for i in list:
print(i)
# string replace
list[1]=list[1].replace("word","world")
for i in list:
print(i)
# recommend re.split, no string.split
str="hello world \n welcome"
print(str.split(' '))
print(str.split('\n'))
# re.split
str="hello world \n welcome"
print(re.split(r'\s+',str))
print(re.split(r' |\n',str))
print(re.split(r'\n+',str))
print(re.split(r'\w+',str))
# shell command in common
os.system(r'rm -f out.log')
# shell command should return value
P=os.popen('ls -al','r')
output=P.read()
print(output)
P.close
# for debug
print(sys._getframe().f_lineno)
os.system(r'rm -f out.log')
# re.search()
# re.sub()
# like perl process file with regexp
# read file from setup.py
# write file to out.log
F=open("setup.py","r")
F1=open("out.log","w")
for line in F:
!!!!!!!!!!!!!Match!!!!!!!!!!! matchObj=re.search(r'(.*)run(.*)',line)
if matchObj:
print("!!!!!!!!!!!!!Match!!!!!!!!!!!",matchObj.group())
!!!!!!!!!!!!!Match!!!!!!!!!!! matchObj_sub=re.sub(r'(.*)run(.*)',r'\1rruunn\2',line)
F1.write(matchObj_sub)
else:
print(line,end='') # end='' can ignore "\n" from variable "line"
pass
F.close
F1.close
qilei@GKCSX091 MINGW32 ~/project/studyEnglish (master)
$ cat out.log
matchObj=re.search(r'(.*)rruunn(.*)',line)
matchObj_sub=re.sub(r'(.*)rruunn(.*)',r'\1rruunn\2',line)
qilei@GKCSX091 MINGW32 ~/project/studyEnglish (master)
$