26 lines
932 B
Python
26 lines
932 B
Python
from fnmatch import fnmatch, fnmatchcase
|
||
|
||
if __name__ == '__main__':
|
||
# 如果想用shell通配符做字符串匹配,可以用上面的这两个函数:
|
||
is_txt = fnmatch("suka.txt", "*.txt")
|
||
print(is_txt)
|
||
is_txt = fnmatchcase("suka.txt", "??ka.txt")
|
||
print(is_txt)
|
||
|
||
# 注意,fnmatchcase不会对输入的name进行大小写标准化,而fnmatch会对输入标准化后再进行匹配
|
||
# 需要小心的是,大小写标准化的模式与底层文件系统相同,比如windows不需要区分大小写,但是mac要
|
||
|
||
address = [
|
||
'5412 N CLARK ST',
|
||
'1060 W ADDISON ST',
|
||
'1039 W GRANVILLE AVE',
|
||
'2122 N CLARK ST',
|
||
'4802 N BROADWAY'
|
||
]
|
||
|
||
# 可以用正则进行筛选
|
||
ST = [addr for addr in address if fnmatchcase(addr, "* ST")]
|
||
print(ST)
|
||
NUM_S = [addr for addr in address if fnmatchcase(addr, '54[0-9][0-9] *CLARK*')]
|
||
print(NUM_S)
|
||
|