对于(x,y)给定的范围,我如何返回x,y中的数字列表
我有一个名为Posture0 Posture 1 posture 2等的文件列表,最高为4000。我想使用一个范围的函数,并为其围绕它提供文件模式。之所以尝试此操作的原因,我可能想获得某些文件范围(1-300),(2-219)以实现这一目标,我到目前为止尝试了这些文件:
自动化regex搜索模式。
我找到了自动化模式搜索的模块RGXG。对于0-180,它产生了此结果。
(180|1[0-7][0-9]|0?[0-9]{1,2})
所以这是我的正则。
posture+(180|1[0-7][0-9]|0[0-9]{1,2})+.jpg
这是不起作用的,因为它应该得到诸如Posture0和Posture180.jpg之类的文件。尽管丢失了0-100,但它也得到了它们,并且在此正则表达式中也发现了1000之类的图案。
后来,我在Python代码上运行了此模式。
import re
rootdir = "postures"
regex = re.compile('posture+(180|1[0-7][0-9]|0[0-9]{1,2})+.jpg')
for root, dirs, files in os.walk(rootdir):
for file in files:
if regex.match(file):
print(file)
它返回文件,但也返回文件0-1500,并且不会在0-99之间返回数字。
我也搜索了Glob,但似乎我找不到这样的功能。
编辑:由于我的问题的反馈不清楚。我会尝试澄清它。
问题: 有没有办法自动化正则搜索字符串?自从我提到的案件没有被捕获以来,我尝试过的那个对我来说并不好。 谢谢 :)
I have got a list of files named posture0 posture 1 posture 2 etc up to 4000. I want to use a function that takes this range and gives me the file pattern around it. The reason why I tried this I might want to get certain ranges of files (1-300),(2-219) In order to achieve this I have tried these so far:
Automate a regex search pattern.
I have found the module rgxg which does automate the pattern searching. For 0-180 it generated this result.
(180|1[0-7][0-9]|0?[0-9]{1,2})
So this was my regex.
posture+(180|1[0-7][0-9]|0[0-9]{1,2})+.jpg
This didn't work because it should have gotten files like posture0 and to posture180.jpg. It got them although 0-100 was missing and also patterns like 1000 were also found with this regex.
And later on I ran this pattern on a python code.
import re
rootdir = "postures"
regex = re.compile('posture+(180|1[0-7][0-9]|0[0-9]{1,2})+.jpg')
for root, dirs, files in os.walk(rootdir):
for file in files:
if regex.match(file):
print(file)
It returns files but it also returns file 0-1500 and it doesn't return numbers between 0-99.
I have also searched glob but it seems I can't find such functionality on it.
Edit:Since I have had the feedback of my question being not clear.I will try to clarify it.
The Question:
Is there a way to automate the search strings in regex? The one I have tried didn't work well for me since the cases I mentioned weren't captured.
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我认为您的问题的解决方案。它实现了通往目录,较低值和上值的路径的函数,然后返回包含由下层和上值定义的范围内的所有文件名。
Here's a solution to what I think is your question. It implements a function that takes a path to a directory, a lower value and an upper value, then returns all filenames that contain the number within range defined by the lower and upper values.