简体   繁体   English

从python中的文件名中提取数字

[英]Extract number from file name in python

I have a directory where I have many data files, but the data file names have arbitrary numbers. 我有一个目录,其中有许多数据文件,但是数据文件名具有任意数字。 For example 例如

data_T_1e-05.d
data_T_7.2434.d
data_T_0.001.d

and so on. 等等。 Because of the decimals in the file names they are not sorted according to the value of the numbers. 由于文件名中的小数,它们没有根据数字的值排序。 What I want to do is the following: I want to open every file, extract the number from the file name, put it in a array and do some manipulations using the data. 我要执行的操作如下:我想打开每个文件,从文件名中提取数字,将其放入数组中,然后使用数据进行一些操作。 Example: 例:

a = np.loadtxt("data_T_1e-05.d",unpack=True)
res[i][0] = 1e-05
res[i][1] = np.sum[a]

I want to do this for every file by running a loop. 我想通过运行循环为每个文件执行此操作。 I think it could be done by creating an array containing all the file names (using import os ) and then doing something with it. 我认为可以通过创建一个包含所有文件名的数组(使用import os ),然后对其进行处理来完成。 How can it be done? 如何做呢?

If your files all start with the same prefix and end with the same suffix, simply slice and pass to float() : 如果您的文件都以相同的前缀开头并以相同的后缀结尾,则只需切片并传递给float()

number = float(filename[7:-2])

This removes the first 7 characters (ie data_T_ ) and the last 2 ( .d ). 这将删除前7个字符(即data_T_ )和后2个字符( .d )。

This works fine for your example filenames: 这适用于您的示例文件名:

>>> for example in ('data_T_1e-05.d', 'data_T_7.2434.d', 'data_T_0.001.d'):
...     print float(example[7:-2])
... 
1e-05
7.2434
0.001
import os
# create the list containing all files from the current dir
filelistall = os.listdir(os.getcwd())
# create the list containing only data files. 
# I assume that data file names end with ".d"
filelist = filter(lambda x: x.endswith('.d'), filelistall)
for filename in filelist:
   f = open(filename, "r")
   number = float(filename[7:-2])
   # and any other code dealing with file
   f.close()

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

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