Python - 如何使用 stdin/stdout 在命令行上解析 argv? [英] Python - How to parse argv on the command line using stdin/stdout?

查看:58
本文介绍了Python - 如何使用 stdin/stdout 在命令行上解析 argv?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手.我查看了有关此的教程,但我越来越困惑.但我想要做的是使用 stdin 和 stdout 来接收数据,通过参数传递它并打印输出结果.

I'm new to programming. I looked at tutorials for this, but I'm just getting more confused. But what I'm trying to do is use stdin and stdout to take in data, pass it through arguments and print out output results.

所以基本上,在命令行上,用户将输入 和 一个参数.

So basically,on the command line, the user will input the and an argument.

参数是:

i = sys.argv [1]

f = sys.argv [2]

w = sys.argv [3]

然后使用 if/else 程序将根据上面选择的参数执行一些内容.

Then using if/else the program will execute some stuff based on which argument chosen above.

即:在命令行上,用户将输入脚本名称和f(对于sys.argv[2:2])

i.e: On the command line the user will enter the script name and f (for sys.argv[2:2])

$ test.py f

.

if sys.argv == i:
     #execute some stuff

elif sys.argv == f:
      #execute some stuff

else: 
     sys.argv == w
     #execute some stuff

使用 stdin/stdout 如何创建这个开关,程序根据选择的 argv 执行一段代码?任何输入将不胜感激.

With stdin/stdout how can I create this switch where the program executes one piece of the code based on which argv is chosen? Any input will be greatly appreciated.

推荐答案

看起来您对 sys.argv 有点困惑.它是您在启动程序时为程序提供的参数列表.所以如果你执行 python program.py f 它将是 ["program.py", "f"].如果你作为 python program.py f w i 执行它,它将是 ["program.py", "f", "w", "i"].所以你展示的代码:

It looks like you are a bit confused about sys.argv. It is a list of the parameters you gave to your program when you started it. So if you execute python program.py f it will be ["program.py", "f"]. If you execute it as python program.py f w i it will be ["program.py", "f", "w", "i"]. So the code you showed:

i = sys.argv[1]
f = sys.argv[2]
w = sys.argv[3]

如果调用的程序参数少于 3 个,则会抛出异常.

will throw an exception if you call the program with less than 3 parameters.

有一些库可以帮助您解析参数,例如 argparse点击.但对于简单的情况,仅使用 sys.argv 可能更容易.

There are some libraries to help you with parsing parameters like argparse or click. But for simple cases just using sys.argv is probably easier.

看起来您希望程序以三种模式运行:i、f 和 w.

It looks like you want your program to operate in three modes: i, f, and w.

if len(sys.argv) > 2:
    print("Please only call me with one parameter")
    sys.exit()

if sys.argv[1] == "f":
    #do some stuff
elif sys.argv[1] == "i":
    #do some other stuff
elif sys.argv[1] == "w":
    #do some more other stuff
else:
    print("Only accepted arguments are f, i and w")
    sys.exit()

您可以通过 printsys.stdout.write() 写入标准输出,其中第一个将为您输入的每个字符串添加换行符.

You can write to stdout via print or sys.stdout.write() where the first one will add a linebreak to each string you input.

如果你想让用户交互输入一些东西,你应该使用 input() (raw_input() in python2. there input()> 将语句评估为您几乎总是不想要的 Python 代码).

If you want the user to interactively input something, you should use input() (raw_input() in python2. There input() evaluates the statement as python code which you almost always don't want).

如果你想用大量数据做一些事情,你可能最好将路径传递给你的程序,然后读入一个文件.你也可以通过 sys.stdin.read()<使用 stdin/code> 但是你想通过管道 some-other-program | 传递一些东西.python program.py f 或读取文件 python program.py f <;文件.txt.(理论上您也可以使用 stdin 来读取交互式数据,但不要这样做,而是使用输入.)

If you want to do something with lots of data you are probably best off if you pass a path to your program and then read a file in. You can also use stdin via sys.stdin.read() but then you want to pass something in there either via a pipe some-other-program | python program.py f or reading a file python program.py f < file.txt. (Theoretically you could also use stdin to read interactive data but don't do that, use input instead.)

这篇关于Python - 如何使用 stdin/stdout 在命令行上解析 argv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆