简体   繁体   English

如何修改 python 脚本以使用外部文件作为命令行中的输入?

[英]How do I modify a python script to use an external file as the input in the command line?

I am very new to coding so excuse any jargon errors in my questions.我对编码很陌生,所以请原谅我的问题中的任何术语错误。 I'm wondering how to modify a script so that when I type the command line with different file names, it returns different calculations.我想知道如何修改脚本,以便当我使用不同的文件名键入命令行时,它会返回不同的计算。 Here is the expected outcome if I do the assignment correctly:如果我正确完成任务,这是预期的结果:

% chmod a+x argv.py
% python Shannon_readfile.py populationA.txt
24.0
1.51309299164
% python Shannon_readfile.py populationB.txt 
24.0
0.681610269053

Essentially, there is the file "Shannon_argv.py", which looks like this:本质上,有一个文件“Shannon_argv.py”,它看起来像这样:

import sys
import math
N=0
H=0

#Calculate N
for i in P:
     a=float(i)
     N=N+a
print(N)

#Calculate H
for i in P:
     a=float(i)
     pa=(a/N)*math.log(a/N)
     H=H+pa
print(-H)

and I have to modify it to by creating an empty list right after the "import math" line, and then by creating a new list P after the "H=0" line.我必须通过在“导入数学”行之后创建一个空列表来修改它,然后在“H = 0”行之后创建一个新列表 P 。

There is another file, readfile_countline.py, that I was told I need to open and take a value from each line, and add it to the list P, and repeat that for all lines, but I do not know what this means, or where to add, etc. I was also told the L.append(x) manipulation would come in handy.还有另一个文件 readfile_countline.py,有人告诉我需要打开并从每一行中取出一个值,然后将其添加到列表 P 中,然后对所有行重复此操作,但我不知道这是什么意思,或者在哪里添加等。我还被告知 L.append(x) 操作会派上用场。

I am using emacs to edit these python scripts in a Linux shell.我正在使用 emacs 在 Linux shell 中编辑这些 python 脚本。 I would really appreciate any help/direction I can get asap.我真的很感激我能尽快得到的任何帮助/方向。 Thank you!谢谢!

To change which file you use from the command line parameters, you can use filename = sys.argv[1] to get the filename from the argument vector.要从命令行参数更改您使用的文件,您可以使用filename = sys.argv[1]从参数向量中获取文件名。 After that, you can open the file, read its contents, and do the processing you need.之后,您可以打开文件,读取其内容,并进行所需的处理。 Assuming from context that your text files contains a single numerical value per line, you could try something like the following:根据上下文假设您的文本文件每行包含一个数值,您可以尝试以下操作:

# Standard library imports.
import math
import sys

# Gets the filename from the command line arguments.
filename = sys.argv[1]

# Reads the data file.
with open(filename, "r") as file:

    P = file.readlines()

# Initializes N and H.
N = 0
H = 0

# Calculates N and H.
for i in P:

    a = float(i)

    N += a
    H += (a / N) * math.log(a / N)

# Prints the results.
print(N)
print(-H)

暂无
暂无

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

相关问题 使用命令行输入修改在终端上运行的 python 脚本以在 IDE 中工作 - Modify a python script that runs on terminal with command line input to work in an IDE 如何从Linux命令行输入到我的Python脚本中? - How do I get input from linux command line into my Python script? 如何在命令行上接受用户输入的python脚本而不是提示 - How do I accept user input on command line for python script instead of prompt 如何在python文件中使用命令行调用可以工作的python脚本? - How do I call a python script that works using command line inside my python file? 我如何在python脚本中使用speedtest-cli或任何替代方法而不是命令行? - How do i use speedtest-cli or any alternative in python script instead of command line? 如何创建一个脚本,该脚本将接收用户输入并在使用python 3.6的终端命令中使用该脚本? - How do I create a script that will take user input and use it in a terminal command using python 3.6? 如何在带有嵌入式文本文件的命令行中运行Python脚本? - How do I run a Python script in the command line with an embedded text file? 如何运行我的 Python 脚本? 为什么命令行告诉我“没有这样的文件或目录”? - How do I run my Python script? Why does the command line tell me "no such file or directory"? 如何为从批处理文件运行的python脚本传递变量命令行参数? - How do I pass variable command line arguments for a python script, that runs from a batch file? 我如何使用 python 脚本中的命令行 - How can i use command line from python script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM