当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- File初相识
- 想花钱速学互联网行业,大概花两三个月的时间,出来好找工作吗
- SEO界,值得收藏的10条金玉良言有哪些?
- gnu汇编语言使用内联汇编 扩展asm
- Collection of blockchain theory [31]
- Version 4.5.7 of swoole was released, and the -- enable swote JSON compilation option was added
- Looking for a small immutable dictionary with better performance
- 从大专生到蚂蚁金服CTO,他写下“支付宝”第一行代码:逆风的方向,更适合飞翔!...
- csdn bug6:待加
- Taulia推出国际支付条款数据库
猜你喜欢

监控系统选型,这篇不可不读!

《Python Cookbook 3rd》笔记(2.3):用Shell通配符匹配字符串
![[technical course] peerconnection in webrtc self built by visual studio 2017_ The client program reported an external symbol error that LNK2019 could not resolve](/img/3b/00bc81122d330c9d59909994e61027.jpg)
[technical course] peerconnection in webrtc self built by visual studio 2017_ The client program reported an external symbol error that LNK2019 could not resolve

express -- 学习笔记(慕课)

MFC界面开发帮助文档——BCG如何在工具栏上放置控件

The high pass snapdragon 875 has won the title of Android processor, but the external 5g baseband has become its disadvantage

SEO industry, what are the 10 pieces of good advice worth collecting?

What should micro service authorization do?

CSDN bug11: to be added

csdn bug3:待加
随机推荐
Leetcode 1-sum of two numbers
ASP.NET Core framework revealed
刷题到底有什么用?你这么刷题还真没用
Summary of basic concepts and common operations of elasticsearch cluster (recommended Collection)
我手撸了一个划线翻译工具!
Jsliang job series - 09 - hand written shallow copy and deep copy
为什么要谨慎使用Arrays.asList、ArrayList的subList?
CSDN bug6: to be added
ASP.NET Core framework revealed [blog Summary - continuous update]
SEO界,值得收藏的10条金玉良言有哪些?
[paper reading notes] large scale heterogeneous feature embedding
A professional tour -- a quick tour of GitHub hot spots Vol.45
Bartender2021 realizes secure remote label printing, new year-end release
Centos7 Rsync + crontab scheduled backup
用python猜测一个数字是集合里面哪些数字相加求和而来的
[paper reading notes] community oriented attributed network embedding
专业之旅——GitHub 热点速览 Vol.45
Leetcode 5561. Get the maximum value in the generated array
CSDN bug5: to be added
[论文阅读笔记] Community-oriented attributed network embedding