使用通配符在列表中查找字符串 [英] Find strings in list using wildcard

查看:59
本文介绍了使用通配符在列表中查找字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用通配符在列表中查找某些文件名.

I am looking for some file names in a list using a wildcard.

from datetime import date
dt = str("RT" + date.today().strftime('%d%m'))
print dt # RT0701

基本上我需要找到此模式 dt +"* .txt" :

Basically I need to find this pattern dt + "*.txt" :

RT0701*.txt

在此列表中:

l = ['RT07010534.txt', 'RT07010533.txt', 'RT02010534.txt']

我该如何使用for循环?

How can i do that with a for loop?

推荐答案

您可以使用

You can use fnmatch.filter() for this:

import fnmatch
l = ['RT07010534.txt', 'RT07010533.txt', 'RT02010534.txt']
pattern = 'RT0701*.txt'
matching = fnmatch.filter(l, pattern)
print(matching)

输出:

['RT07010534.txt', 'RT07010533.txt']

这篇关于使用通配符在列表中查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆