如何读取连续的.plt文件并将其存储 [英] How to read consecutive .plt files and store them
问题描述
我有1000个要使用python读取的文件.以前,我只有74岁,我只是一个一个地阅读它们,但是现在要做的太多了.
I have 1000 files that I want to read with python. Before, I had 74 and I just read them one by one, but now there are too much to do so.
data_1 = np.genfromtxt('test4-1.000001.plt', delimiter=' ', skip_header=3)
data_2 = np.genfromtxt('test4-1.000002.plt', delimiter=' ', skip_header=3)
data_3 = np.genfromtxt('test4-1.000003.plt', delimiter=' ', skip_header=3)
data_4 = np.genfromtxt('test4-1.000004.plt', delimiter=' ', skip_header=3)
data_5 = np.genfromtxt('test4-1.000005.plt', delimiter=' ', skip_header=3)
data_6 = np.genfromtxt('test4-1.000006.plt', delimiter=' ', skip_header=3)
data_7 = np.genfromtxt('test4-1.000007.plt', delimiter=' ', skip_header=3)
.
.
.
.
.
data_73 = np.genfromtxt('test4-1.000073.plt', delimiter=' ', skip_header=3)
data_74 = np.genfromtxt('test4-1.000074.plt', delimiter=' ', skip_header=3)
我尝试使用循环,但是没有用.我试图使自己成为弦乐,但仍然无法正常工作
I have tried to use a loop for, but it doesn't work. I tried to make i be a string, but still didn't work
for i in range(1:1000):
data_'i' = np.genfromtxt('test4-1.00000'i'.plt', delimiter=' ',skip_header=3)
非常感谢您的帮助
推荐答案
您虽然有一个正确的主意,但是您犯了一些语法错误,而且您似乎需要了解列表.
You kinda have the right idea but you made a few syntax mistakes, and it looks like you need to learn about lists.
您应该做的是列出所有数据.让我们称之为 data
.
What you should do is make a list of all your data. Lets call it data
.
我们这样定义一个空: data = []
We define an empty like like this: data = []
要添加到列表中,我们使用如下的append命令:
To add to a list we use the append command like this:
data.append(np.genfromtxt('test4-1.000001.plt', delimiter=' ',skip_header=3))
最后一件事是您试图错误地连接字符串.在python中,您使用 +
连接字符串,因此以下行:'test4-1.00000'i'.plt'
应该为'test4-1.'+ str(i).zfill(6)+'.plt'
.请注意,我必须首先将其转换为字符串,并且还使用了 zfill
函数将其填充为零.
The last thing is you were trying to concatenate your strings wrong. In python you concatenate strings with a +
so this line: 'test4-1.00000'i'.plt'
should be 'test4-1.'+str(i).zfill(6)+'.plt'
. Note I had to turn it into a string first and I also used the zfill
function to pad it with zeros.
将所有这些放在一起,我们就有了代码
Putting all this together we have the code
data = []
for i in range(1:1000):
data.append(np.genfromtxt('test4-1.'+str(i).zfill(6)+'.plt', delimiter=' ',skip_header=3))
这将为您提供一系列数据,您可以使用语法 data [23]
调用这些数据以获取第24个数据集(列表索引从0开始).
This will leave you with a list of data that you can call from using the syntax data[23]
to get the 24th data set (list indexing starts at 0).
希望这对您有帮助!
这篇关于如何读取连续的.plt文件并将其存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!