当前位置:网站首页>《Python Cookbook 3rd》笔记(2.3):用Shell通配符匹配字符串
《Python Cookbook 3rd》笔记(2.3):用Shell通配符匹配字符串
2020-11-10 10:51:00 【巨輪】
用 Shell 通配符匹配字符串
问题
你想使用 Unix Shell 中常用的通配符 (比如 *.py , Dat[0-9]*.csv 等) 去匹配文本字符串
解法
fnmatch 模块提供了两个函数—— fnmatch() 和 fnmatchcase() ,可以用来实现这样的匹配。用法如下:
>>> 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() 函数使用底层操作系统的大小写敏感规则 (不同的系统是不一样的) 来匹配模式。比如:
>>> # On OS X (Mac)
>>> fnmatch('foo.txt', '*.TXT')
False
>>> # On Windows
>>> fnmatch('foo.txt', '*.TXT')
True
>>>
如果你对这个区别很在意,可以使用 fnmatchcase() 来代替。它完全使用你的模式大小写匹配。比如:
>>> fnmatchcase('foo.txt', '*.TXT')
False
>>>
这两个函数通常会被忽略的一个特性是在处理非文件名的字符串时候它们也是很有用的。比如,假设你有一个街道地址的列表数据:
addresses = [
'5412 N CLARK ST',
'1060 W ADDISON ST',
'1039 W GRANVILLE AVE',
'2122 N CLARK ST',
'4802 N BROADWAY'
]
你可以像这样写列表推导:
>>> 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']
>>>
讨论
fnmatch() 函数匹配能力介于简单的字符串方法和强大的正则表达式之间。如果在数据处理操作中只需要简单的通配符就能完成的时候,这通常是一个比较合理的方案。
版权声明
本文为[巨輪]所创,转载请带上原文链接,感谢
https://my.oschina.net/jallenkwong/blog/4710735
边栏推荐
- Swoole v4.5.7 版本发布,新增--enable-swoole-json编译选项
- Mongodb index management of distributed document storage database
- [论文阅读笔记] Community-oriented attributed network embedding
- Graph undirected graph
- csdn bug11:待加
- [论文阅读笔记] A Multilayered Informative Random Walk for Attributed Social Network Embedding
- CSDN bug8: to be added
- An unsafe class named unsafe
- .MD语法入门
- 网络安全工程师演示:原来***是这样控制你的服务器的!(下)
猜你喜欢

上线1周,B.Protocal已有7000ETH资产!

Thinking about competitive programming: myths and shocking facts

One of the 10 Greatest formulas in the world is well known

nodejs 个人学习笔记(imook)pm2

仅发送options请求,没有发送post解决方案

CSDN bug10: to be added

【技术教程】C#控制台调用FFMPEG推MP4视频文件至流媒体开源服务平台EasyDarwin过程

Commodity management - merge purchase demand into purchase order

ServiceManagerProxy中mRemote变量指的什么?

专业之旅——GitHub 热点速览 Vol.45
随机推荐
[paper reading notes] community oriented attributed network embedding
注册滴滴加不上车怎么办?要怎么处理?
Leetcode 1-sum of two numbers
csdn bug5:待加
CentOS7本地源yum配置
他把闲鱼APP长列表流畅度翻了倍
What can I do if I can't register didi? How to deal with it?
《Python Cookbook 3rd》笔记(2.4):字符串匹配和搜索
Factory approach model
CSDN bug4: to be added
自定义注解!绝对是程序员装逼的利器!!
从零开始学习 YoMo 系列教程:开篇
[论文阅读笔记] RoSANE, Robust and scalable attributed network embedding for sparse networks
ASP.NET Core framework revealed [blog Summary - continuous update]
[paper reading notes] rosane, robust and scalable attributed network embedding for sparse networks
Yixian e-commerce prospectus of perfect diary parent company: focusing on marketing and ignoring R & D, with a loss of 1.1 billion in the first three quarters
关于centos启动报错:Failed to start Crash recovery kernel arming的解决方案
专业之旅——GitHub 热点速览 Vol.45
走进C# abstract,了解抽象类与接口的异同
【操作教程 】国标GB28181协议安防视频平台EasyGBS订阅功能介绍及开启步骤