[英]Find specific substring while iterating through multiple file names
在遍历文件时,我需要找到大量文件的标识号。
文件名将加载到列表中,如下所示:
ID322198.nii
ID9828731.nii
ID23890.nii
FILEID988312.nii
因此,解决此问题的最佳方法是找到位于ID
和.nii
之间的.nii
因为位数不同,所以我不能简单地选择文件名的[-10:-4]
。 有任何想法吗?
要找到ID
和.nii
的位置,可以使用python的index()
函数
for line in file:
idpos =
nilpos =
data =
或作为整数列表:
[ int(line[line.index("ID")+1:line.index(".nii")]) for line in file ]
您可以使用正则表达式(在此处查看实际操作):
import re
files = ['ID322198.nii','ID9828731.nii','ID23890.nii','FILEID988312.nii']
[re.findall(r'ID(\d+)\.nii', file)[0] for file in files]
返回值:
['322198', '9828731', '23890', '988312']
使用rindex
:
s = 'ID322198.nii'
s = s[s.rindex('D')+1 : s.rindex('.')]
print(s)
返回值:
322198
然后将此正弦值应用于字符串列表。
for name in files:
name = name.replace('.nii', '')
id_num = name.replace(name.rstrip('0123456789'), '')
工作原理:
# 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, '')
看来您可以过滤出数字,如下所示:
digits = ''.join(d for d in filename if d.isdigit())
只要文件名中没有其他数字(例如带.1后缀的备份或类似的东西),该方法就可以很好地工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.