python学习——fnmatch

来源:互联网 发布:特朗普 中美关系 知乎 编辑:程序博客网 时间:2024/05/16 13:51

fnmatch 模块使用模式来匹配文件名

>>> from fnmatch import fnmatch, fnmatchcase>>> fnmatch('foo.txt', '*.txt')True>>> fnmatch('foo.txt', '?oo.txt')True>>> fnmatch('Dat45.csv', 'Dat[0-9]*')True>>> names = ['Dat1.csv', 'Dat2.csv', 'config.ini', 'foo.py']>>> [name for name in names if fnmatch(name, 'Dat*.csv')]['Dat1.csv', 'Dat2.csv']>>>
addresses = [    {'address':'5412 N CLARK', 'date': '07/01/2012'},    {'address':'5148 N CLARK', 'date': '07/04/2012'},    {'address':'5800 E 58TH', 'date': '07/02/2012'},    {'address':'2122 N CLARK', 'date': '07/03/2012'},    {'address':'5645 N RAVENSWOOD', 'date': '07/02/2012'},    {'address':'1060 W ADDISON', 'date': '07/02/2012'},    {'address':'4801 N BROADWAY', 'date': '07/01/2012'},    {'address':'1039 W GRANVILLE', 'date': '07/04/2012'},]>>> from fnmatch import fnmatchcase>>> [addr for addr in addresses if fnmatchcase(addr, '* ST')]['5412 N CLARK ST', '1060 W ADDISON ST', '2122 N CLARK ST']>>> [addr for addr in addresses if fnmatchcase(addr, '54[0-9][0-9] *CLARK*')]['5412 N CLARK ST']>>>
0 0