简体   繁体   English

在遍历多个文件名的同时查找特定的子字符串

[英]Find specific substring while iterating through multiple file names

I need to find the identification number of a big number of files while iterating throught them. 在遍历文件时,我需要找到大量文件的标识号。

The file names are loaded onto a list and look like: 文件名将加载到列表中,如下所示:

ID322198.nii
ID9828731.nii
ID23890.nii
FILEID988312.nii

So the best way to approach this would be to find the number that sits between ID and .nii 因此,解决此问题的最佳方法是找到位于ID.nii之间的.nii

Because number of digits varies I can't simply select [-10:-4] of thee file name. 因为位数不同,所以我不能简单地选择文件名的[-10:-4] Any ideas? 有任何想法吗?

to find the position of ID and .nii , you can use python's index() function 要找到ID.nii的位置,可以使用python的index()函数

for line in file:
    idpos = 
    nilpos = 
    data = 

or as a list of ints: 或作为整数列表:

[ int(line[line.index("ID")+1:line.index(".nii")]) for line in file ]

You can use a regex (see it in action here ): 您可以使用正则表达式(在此处查看实际操作):

import re

files = ['ID322198.nii','ID9828731.nii','ID23890.nii','FILEID988312.nii']

[re.findall(r'ID(\d+)\.nii', file)[0] for file in files]

Returns: 返回值:

['322198', '9828731', '23890', '988312']

Using rindex : 使用rindex

s = 'ID322198.nii'
s = s[s.rindex('D')+1 : s.rindex('.')]
print(s)

Returns: 返回值:

322198

Then apply this sintax to a list of strings. 然后将此正弦值应用于字符串列表。

for name in files:
    name = name.replace('.nii', '')
    id_num = name.replace(name.rstrip('0123456789'), '')

How this works: 工作原理:

# example
name = 'ID322198.nii'

# remove '.nii'. -> name1 = 'ID322198'
name1 = name.replace('.nii', '') 

# strip all digits from the end. -> name2 = 'ID'
name2 = name1.rstrip('0123456789') 

# remove 'ID' from 'ID322198'. -> id_num = '322198'
id_num = name1.replace(name2, '')

It seems like you could filter the digits out, like this: 看来您可以过滤出数字,如下所示:

digits = ''.join(d for d in filename if d.isdigit())

That will work nicely as long as there are no other digits in the filename (eg backups with a .1 suffix or something). 只要文件名中没有其他数字(例如带.1后缀的备份或类似的东西),该方法就可以很好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM