I need to explain more clearly because from the title you can't completely comprehend my needs...
我需要更清楚地解释一下,因为从标题中你无法完全理解我的需求......
File = input ("Insert the name of the file: ")
IV = np.genfromtxt('D:\User\Python\Programms_Python\programms\%d.txt',
File, usecols=(1,5,2,5))
I need to insert from command line the name of the file I want to analyze, because I have to do multiple analyses on many different .txt
files and inserting their names directly from the command line would make my job much faster. Can you suggest a way to insert it inside the function genfromtxt
? Is it possible?
我需要从命令行插入我想要分析的文件的名称,因为我必须对许多不同的.txt文件进行多次分析,直接从命令行插入它们的名称会使我的工作更快。你能建议一种方法将它插入函数genfromtxt吗?可能吗?
genfromtxt is a function inside numpy and it allows you to copy the values of a column in your txt inside an array, using usecols you can ask which column to copy in the array which this way can become a multidimensional array, like in this case IV is a four dimensional array.
genfromtxt是numpy中的一个函数,它允许你复制你的txt中一个列的值,使用usecols你可以询问哪个列要复制到数组中,这样就可以成为一个多维数组,就像在这种情况下一样是一个四维数组。
Trying the way I showed above, genfromtxt
doesn't recognize the %d -> File
(the names of the .txt
files are just numbers and I tried also with %s
but it didn't change) so please.. HELP
尝试我上面显示的方式,genfromtxt无法识别%d - >文件(.txt文件的名称只是数字,我也尝试使用%s,但它没有改变)所以请...帮助
1 个解决方案
#1
Probably the easiest way of doing this would be the get the command line arguments via sys.argv
, then to loop over them, passing them one at a time to your function genfromtext
(I have no idea what genfromtext
is, by the way. Your question makes no mention of where it comes from. But from your usage, I assume it's a function in a 3rd party module np
which you also told us nothing about.) So, anyways, your code would look something like this:
可能最简单的方法是通过sys.argv获取命令行参数,然后循环遍历它们,一次一个地传递给你的函数genfromtext(顺便说一下,我不知道genfromtext是什么。你的问题没有提到它来自哪里。但是根据你的用法,我认为它是第三方模块np中的一个函数你也没有告诉过我们。)所以,无论如何,你的代码看起来像这样:
from sys import argv
for arg in argv[1:]:
IV = np.genfromtext(arg, usecols=(1,5,2,5))
The [1:]
after argv
is so you don't get the first command line argument, which would just be the name of your Python script file.
argv之后的[1:]是你没有得到第一个命令行参数,它只是你的Python脚本文件的名称。
#1
Probably the easiest way of doing this would be the get the command line arguments via sys.argv
, then to loop over them, passing them one at a time to your function genfromtext
(I have no idea what genfromtext
is, by the way. Your question makes no mention of where it comes from. But from your usage, I assume it's a function in a 3rd party module np
which you also told us nothing about.) So, anyways, your code would look something like this:
可能最简单的方法是通过sys.argv获取命令行参数,然后循环遍历它们,一次一个地传递给你的函数genfromtext(顺便说一下,我不知道genfromtext是什么。你的问题没有提到它来自哪里。但是根据你的用法,我认为它是第三方模块np中的一个函数你也没有告诉过我们。)所以,无论如何,你的代码看起来像这样:
from sys import argv
for arg in argv[1:]:
IV = np.genfromtext(arg, usecols=(1,5,2,5))
The [1:]
after argv
is so you don't get the first command line argument, which would just be the name of your Python script file.
argv之后的[1:]是你没有得到第一个命令行参数,它只是你的Python脚本文件的名称。