当前位置:网站首页>Notes on Python cookbook 3rd (2.3): matching strings with shell wildcards
Notes on Python cookbook 3rd (2.3): matching strings with shell wildcards
2020-11-10 10:51:00 【Giant ship】
use Shell Wildcards match strings
problem
You want to use Unix Shell The common wildcard in ( such as *.py , Dat[0-9]*.csv etc. ) To match text strings
solution
fnmatch Module provides two functions —— fnmatch() and fnmatchcase() , Can be used to achieve such a match . Usage is as follows :
>>> 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']
>>>
fnmatch() Function uses the case sensitive rules of the underlying operating system ( Different systems are different ) To match patterns . such as :
>>> # On OS X (Mac)
>>> fnmatch('foo.txt', '*.TXT')
False
>>> # On Windows
>>> fnmatch('foo.txt', '*.TXT')
True
>>>
If you care about the difference , have access to fnmatchcase() Instead of . It completely uses your pattern case matching . such as :
>>> fnmatchcase('foo.txt', '*.TXT')
False
>>>
One of the features that are often overlooked in these two functions is that they are also useful when dealing with non file name strings . such as , Suppose you have a list of street addresses :
addresses = [
'5412 N CLARK ST',
'1060 W ADDISON ST',
'1039 W GRANVILLE AVE',
'2122 N CLARK ST',
'4802 N BROADWAY'
]
You can write list derivation like this :
>>> 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']
>>>
Discuss
fnmatch() Function matching is between simple string methods and powerful regular expressions . If you only need a simple wildcard to complete the data processing operation , This is usually a reasonable plan .
版权声明
本文为[Giant ship]所创,转载请带上原文链接,感谢
边栏推荐
- getIServiceManager() 源码分析
- 带劲!饿了么软件测试python自动化岗位核心面试题出炉,你全程下来会几个?
- csdn bug1:待加
- CSDN bug8: to be added
- Common skills of JS array
- [paper reading notes] rosane, robust and scalable attributed network embedding for sparse networks
- He doubled the fluency of the long list of idle fish app
- The high pass snapdragon 875 has won the title of Android processor, but the external 5g baseband has become its disadvantage
- CSDN bug6: to be added
- [论文阅读笔记] RoSANE, Robust and scalable attributed network embedding for sparse networks
猜你喜欢
图-无向图
[paper reading notes] a multilayered informational random walk for attributed social network embedding
Summary of basic concepts and common operations of elasticsearch cluster (recommended Collection)
csdn bug10:待加
2020-11-07
[论文阅读笔记] A Multilayered Informative Random Walk for Attributed Social Network Embedding
csdn bug1:待加
Sign in with apple
ElasticSearch 集群基本概念及常用操作汇总(建议收藏)
注册滴滴加不上车怎么办?要怎么处理?
随机推荐
csdn bug3:待加
[论文阅读笔记] Network Embedding with Attribute Refinement
ASP.NET Core框架揭秘[博文汇总-持续更新]
GNU assembly language uses inline assembly to extend ASM
csdn bug7:待加
Taulia launches international payment terms database
Swoole 如何使用 Xdebug 进行单步调试
《Python Cookbook 3rd》笔记(2.3):用Shell通配符匹配字符串
Summary of basic concepts and common operations of elasticsearch cluster (recommended Collection)
CCR coin robot: novel coronavirus pneumonia has accelerated the interest of regulators in CBDC.
CSDN BUG1: to be added
Graph undirected graph
图-无向图
用python猜测一个数字是集合里面哪些数字相加求和而来的
Multibank group announced record financial results with gross profit of $94 million in the first three quarters of 2020
[paper reading notes] rosane, robust and scalable attributed network embedding for sparse networks
Centos7 local source Yum configuration
csdn bug5:待加
Want to spend money to learn the Internet industry quickly, about two or three months, come out to find a job
一不小心画了 24 张图剖析计网应用层协议!