[英]How to load all .txt files in a folder with separate names - Python
I have to create a bunch of numpy arrays by importing all the.txt files from a folder.我必须通过从文件夹中导入所有 the.txt 文件来创建一堆 numpy arrays。 This is the way I'm doing it right now:
这就是我现在正在做的方式:
wil_davide_noIA_IST_nz300 = np.genfromtxt("%s/davide/wil_davide_noIA_IST_nz300.txt" %path)
wig_davide_multiBinBias_IST_nz300 = np.genfromtxt("%s/davide/wig_davide_multiBinBias_IST_nz300.txt" %path)
wig_davide_multiBinBias_PySSC_nz300 = np.genfromtxt("%s/davide/wig_davide_multiBinBias_PySSC_nz300.txt" %path)
wig_davide_noBias_PySSC_nz300 = np.genfromtxt("%s/davide/wig_davide_noBias_PySSC_nz300.txt" %path)
wig_davide_IST_nz300 = np.genfromtxt("%s/davide/wig_davide_IST_nz300.txt" %path)
wig_davide_noBias_IST_nz300 = np.genfromtxt("%s/davide/wig_davide_noBias_IST_nz300.txt" %path)
wig_davide_PySSC_nz300 = np.genfromtxt("%s/davide/wig_davide_PySSC_nz300.txt" %path)
...
from the folder从文件夹
Can I automate the process somehow?我可以以某种方式自动化这个过程吗? Note that I'd like the array to have the same name as the imported file (without the.txt, of course).
请注意,我希望数组与导入的文件具有相同的名称(当然,没有 .txt)。
Thank you very much非常感谢
This is how you can get all the filenames.这是获取所有文件名的方法。
path
is whatever is your file path ( "./"
if in the same directory) path
是您的文件路径(如果在同一目录中,则为"./"
)
import os
path = "./"
filenames = []
for filename in os.listdir(path):
filenames.append[file]
...
Also, you can filter some files with the if-structure provided.此外,您可以使用提供的 if 结构过滤一些文件。 Here I'm selecting only txt files.
这里我只选择 txt 文件。
filter
is some filter you can apply (use "." not in file
to get directories) filter
是您可以应用的一些过滤器(使用"." not in file
中获取目录)
import os
path = "./"
filter = ".txt"
txt_filenames = []
for filename in os.listdir(path):
if filter in file:
txt_filenames.append[filename]
...
A heads up: You should not do this, if there are any security concerns with the files or their names.注意:如果文件或其名称存在任何安全问题,您不应该这样做。
Otherwise you can use the exec
function to get the variables you want.否则,您可以使用
exec
function 来获取您想要的变量。 For a list approach see the other solution.有关列表方法,请参阅其他解决方案。
import os
path_to_folder = "."
command = ""
for f in os.listdir(path_to_folder):
if f.endswith(".txt"):
command += f.replace(".txt", "") + f" = np.genfromtxt('{path_to_folder}/{f}')\n"
exec(command)
Depended on your folder/file structure you would need to change up the code a bit, but that should work like asked.根据您的文件夹/文件结构,您需要稍微更改代码,但这应该像要求的那样工作。
I haven't tested this, but I think this pseudo-code should work - the only thing "pseudo" about it is the hardcoded "dir/to/files"
string, which you'll have to change to the path to the directory containing the text files.我没有对此进行测试,但我认为这个伪代码应该可以工作 - 关于它的唯一“伪”是硬编码的
"dir/to/files"
字符串,您必须将其更改为目录的路径包含文本文件。 In modern Python you would use the pathlib
or glob
standard library modules to iterate over all text files in a given directory.在现代 Python 中,您将使用
pathlib
或glob
标准库模块来遍历给定目录中的所有文本文件。 Creating a variable number of variables, with variable names determined at runtime is never a good idea.创建可变数量的变量,并在运行时确定变量名称绝不是一个好主意。 Instead, keep your numpy arrays in some kind of collection.
相反,请将您的 numpy arrays 保存在某种集合中。 In this case, I would suggest a dictionary, so that you can access the individual numpy arrays by key, where the key is the name of the corresponding file as a string - without the extension of course:
在这种情况下,我建议使用字典,以便您可以通过键访问单个 numpy arrays ,其中键是相应文件的名称作为字符串 - 当然没有扩展名:
def get_kv_pairs():
from pathlib import Path
for path in Path("dir/to/files").glob("*.txt"):
yield path.stem, np.genfromtxt(str(path))
arrays = dict(get_kv_pairs())
print(arrays["wil_davide_noIA_IST_nz300"]) # Here's how you would access the individual numpy arrays.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.