当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- C + + STL container
- Q & A and book donation activities of harbor project are in hot progress
- Call the open source video streaming media platform dawinffc
- Multibank group announced record financial results with gross profit of $94 million in the first three quarters of 2020
- 基于FPGA的MCP4725驱动程序
- Bartender2021 realizes secure remote label printing, new year-end release
- 抖音Api:视频评论列表
- 世界上最伟大的10个公式,其中一个人尽皆知
- Sign in with apple
- Api: tiktok: Video Review List
猜你喜欢
随机推荐
python pip命令的使用
nodejs 个人学习笔记(imook)pm2
想花钱速学互联网行业,大概花两三个月的时间,出来好找工作吗
LeetCode:二叉树(四)
ElasticSearch 集群基本概念及常用操作汇总(建议收藏)
高通骁龙875夺安卓处理器桂冠,但外挂5G基带成为它的弊病
layer.prompt(options, yes) - 输入层
[operation tutorial] introduction and opening steps of easygbs subscription function of national standard gb28181 protocol security video platform
B. protocal has 7000eth assets in one week!
TCP性能分析与调优策略
LeetCode:数组(一)
gnu汇编-基本数学方程-乘法
csdn bug11:待加
基于FPGA的MCP4725驱动程序
The high pass snapdragon 875 has won the title of Android processor, but the external 5g baseband has become its disadvantage
LeetCode 5561. 获取生成数组中的最大值
How to view the establishment of the new retail business department of Alibaba cloud?
Use Python to guess which numbers in the set are added to get the sum of a number
layer.prompt(options, yes) - 输入层
[论文阅读笔记] Network Embedding with Attribute Refinement



