[英]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 ]
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.