当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
猜你喜欢

express -- 学习笔记(慕课)
![[C#.NET 拾遗补漏]11:最基础的线程知识](/img/5f/3db6e0620191da4893ad7b4e2ba064.jpg)
[C#.NET 拾遗补漏]11:最基础的线程知识

CSDN BUG1: to be added

Centos7 rsync+crontab 定时备份

安卓快速关机APP
![[paper reading notes] a multilayered informational random walk for attributed social network embedding](/img/3d/657a60600219ce6cfc514ad1b1bb49.jpg)
[paper reading notes] a multilayered informational random walk for attributed social network embedding

One accidentally drew 24 diagrams to analyze the network application layer protocol!

CSDN bug6: to be added

Key layout of the Central Government: in the next five years, self-reliance and self-improvement of science and technology will be the priority, and these industries will be named

MultiBank Group宣布创纪录的财务业绩,2020年前三季度毛利达到9,400万美元
随机推荐
为什么要谨慎使用Arrays.asList、ArrayList的subList?
自定义注解!绝对是程序员装逼的利器!!
他把闲鱼APP长列表流畅度翻了倍
Centos7 rsync+crontab 定时备份
抖音Api:视频评论列表
First acquaintance of file
GNU assembly language uses inline assembly to extend ASM
专业之旅——GitHub 热点速览 Vol.45
Leetcode: binary tree (4)
Custom annotation! Absolutely is the sharp weapon that programmer installs force!!
用例子理解递归
jsliang 求职系列 - 09 - 手写浅拷贝和深拷贝
LeetCode:二叉树(四)
New feature of ios14 -- development and practice of widgetkit
网络时间服务器(医院时钟系统)相关问题汇总
express -- 学习笔记(慕课)
CSDN BUG1: to be added
Bartender2021 realizes secure remote label printing, new year-end release
【操作教程 】国标GB28181协议安防视频平台EasyGBS订阅功能介绍及开启步骤
Introduction to. MD grammar