文本和字符串(一)
更新信息
2018-11-19 更新 re.split()
分割
split
通過 URL 地址,得到主機名和域名
In [41]: url = 'www.sharkyun.com'
In [42]: host, domain = url.split('.', 1)
In [43]: host
Out[43]: 'www'
In [44]: domain
Out[44]: 'sharkyun.com'
把下面的模塊路徑分隔開來,分別得到包和模塊名
# 下面是目錄結構
mod
└── plugins
└── cpu.py
cpu.py 中有一個類
class CPU:
def __init__(self):
pass
我們可以這樣分隔它
plugin_path = 'mod.plugins.cpu'
mod_path, cls_name = plugin.rsplit('.', 1)
輸出
In [59]: mod_path
Out[59]: 'mod.plugins'
In [60]: cls_name
Out[60]: 'cpu'
也許你會遇到更為復雜的情況, 如:
In [1]: s = 'hello xiguatian; vars, id,def, foo'
想得到所于的英文字符,之用字符串的 split()
方法是不夠的,正確的是使用 re.split()
In [2]: import re
In [3]: re.split(r'[\s;,]+', s)
Out[3]: ['hello', 'xiguatian', 'vars', 'id', 'def', 'foo']
用Shell通配符匹配字符串
如何利用 shell 里的通配符去匹配字符串,比如用 *.log
去匹配 access.log
等。
fnmatch
模塊提供了兩個函數—— fnmatch()
和 fnmatchcase()
,可以用來實現這樣的匹配。
用法如下:
In [3]: fnmatch('cpu.py', '*.py')
Out[3]: True
In [4]: fnmatch('cpu.py', '?pu.py')
Out[4]: True
In [5]: fnmatch('data10.py', 'data[0-9][0-9].py')
Out[5]: True
fnmatch()
函數在類 linux
平臺中是區分大小寫的,而在 Windows
下是不區分的.
# On OS X (Mac)
In [6]: ffnmatch('foo.txt', '*.TXT')
Out[6]: False
# On Windows
In [7]: ffnmatch('foo.txt', '*.TXT')
Out[7]: True
fnmatchcase()
函數可以絕對的區分大小寫
In [14]: fnmatchcase('foo.txt', '*.TXT')
Out[14]: False
注意:上面的第一個參數,對于這兩個函數只是字符串而已,python 不會判斷是否是文件名。所以你若是相對文件名做匹配,請使用
glob
模塊。
假如有如下大學信息
star_rating = [
"北京大學 8星級",
"清華大學 8星級",
"中國人民大學 8星級",
"北京師范大學 6星級",
"北京航空航天大學 6星級",
"北京理工大學 5星級",
"中國農業大學 6星級",
"北京交通大學 4星級",
"北京科技大學 4星級",
"北京協和醫學院 6星級"
]
找到 6 到 8 星級的大學
university = [star for star in star_rating if fnmatchcase(star, '*[6-8]星級') ]
print(university)
輸出結果
['北京大學 8星級',
'清華大學 8星級',
'中國人民大學 8星級',
'北京師范大學 6星級',
'北京航空航天大學 6星級',
'中國農業大學 6星級',
'北京協和醫學院 6星級'
]
搜索替換
replace()
s = "shark www.sharkyun.com"
# 把 shark 換成 xiguatian
s = s.replace('shark', 'xiguatian')
輸入結果
print(s)
xiguatian www.xiguatianyun.com
當然,事情往往不總是都是那么多稱心如意。比如:
將形式為 11/16/2018
的日期字符串改成 2018-11-16
。
這時候,就要使用殺手锏正則模塊 re
re
模塊中的 sub()
函數可以為我們辦的。代碼如下:
import re
text = 'Today is 11/16/2018.'
re.sub(r'(\d+)/(\d+)/(\d+)', r'\3-\1-\2', text)
輸出結果
'Today is 2018-11-16.'
\1
\2
\3
是對前面小括號內模式的匹配結果的都捕獲,就是分組。
如果有個正則的模式可能需要多次使用,那就要考慮先編譯它來提升性能。
import re
datepat = re.compile(r'(\d+)/(\d+)/(\d+)')
datepat.sub(r'\3-\1-\2', text)
假如,想知道有多少替換發生了,可以使用 re.subn() 來代替。比如:
In [31]: text = 'Today is 11/16/2018.'
In [32]: new_text, n = re.subn(r'(\d+)/(\d+)/(\d+)', r'\3-\1-\2', text)
In [33]: new_text
Out[33]: 'Today is 2018-11-16.'
In [34]: n
Out[34]: 1
更加復雜的替換需求:把其中的月份 11
改為 Nov
預備知識:
In [47]: from calendar import month_abbr
In [48]: month_abbr[1]
Out[48]: 'Jan'
In [49]: month_abbr[11]
Out[49]: 'Nov'
month_abbr
是一個可被迭代對對象,里面存放了12
個月份的英文3個字母的縮寫, 第一個位置為空的字符串''
, 我們可以通過索引號1-12
進行分別取值。
開始正題:
更加復雜的替換,可以傳遞一個替換回調函數給 sub()
函數。
import re
from calendar import month_abbr
text = 'Today is 11/16/2018.'
def change_date(m):
mon_name = month_abbr[int(m.group(1))]
return '{} {} {}'.format(m.group(2), mon_name, m.group(3))
new_text = re.sub(r'(\d+)/(\d+)/(\d+)', change_date, text)
輸出結果
print(new_text)
Today is 16 Nov 2018.
替換回調函數的參數是一個
match
對象,也就是match()
返回的對象。
這里我接著使用 group() 方法來提取特定的匹配部分。
回調函數最后返回替換后的字符串。