Python中的命令行输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16179875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Command line input in Python
提问by Viin
Is it possible to run first the program then wait for the input of the user in command line. e.g.
是否可以先运行程序然后等待用户在命令行中的输入。例如
Run...
Process...
Input from the user(in command line form)...
Process...
回答by Mike
It is not at all clear what the OP meant (even after some back-and-forth in the comments), but here are two answers to possible interpretations of the question:
根本不清楚 OP 的意思(即使在评论中反复讨论之后),但这里有两个对问题可能解释的答案:
For interactive user input (or piped commands or redirected input)
用于交互式用户输入(或管道命令或重定向输入)
Use raw_inputin Python 2.x, and inputin Python 3. (These are built in, so you don't need to import anything to use them; you just have to use the right one for your version of python.)
使用raw_input在Python 2.x和input在Python 3(这些都是内置的,所以你不需要输入任何东西来使用它们。你只需要使用适合您Python版本)
For example:
例如:
user_input = raw_input("Some input please: ")
More details can be found here.
可以在此处找到更多详细信息。
So, for example, you might have a script that looks like this
因此,例如,您可能有一个如下所示的脚本
# First, do some work, to show -- as requested -- that
# the user input doesn't need to come first.
from __future__ import print_function
var1 = 'tok'
var2 = 'tik'+var1
print(var1, var2)
# Now ask for input
user_input = raw_input("Some input please: ") # or `input("Some...` in python 3
# Now do something with the above
print(user_input)
If you saved this in foo.py, you could just call the script from the command line, it would print out tok tiktok, then ask you for input. You could enter bar baz(followed by the enter key) and it would print bar baz. Here's what that would look like:
如果你把它保存在 中foo.py,你可以从命令行调用脚本,它会打印出来tok tiktok,然后要求你输入。您可以输入bar baz(后跟输入键),它会打印bar baz. 这就是它的样子:
$ python foo.py
tok tiktok
Some input please: bar baz
bar baz
Here, $represents the command-line prompt (so you don't actually type that), and I hit Enterafter typing bar bazwhen it asked for input.
在这里,$代表命令行提示符(所以你实际上没有输入),当它要求Enter输入bar baz时我在输入后点击。
For command-line arguments
对于命令行参数
Suppose you have a script named foo.pyand want to call it with arguments barand bazfrom the command line like
假设您有一个名为的脚本,foo.py并希望使用参数bar和baz从命令行调用它,例如
$ foo.py bar baz
(Again, $represents the command-line prompt.) Then, you can do that with the following in your script:
(同样,$代表命令行提示符。)然后,您可以在脚本中使用以下内容:
import sys
arg1 = sys.argv[1]
arg2 = sys.argv[2]
Here, the variable arg1will contain the string 'bar', and arg2will contain 'baz'. The object sys.argvis just a list containing everything from the command line. Note that sys.argv[0]is the name of the script. And if, for example, you just want a single list of all the arguments, you would use sys.argv[1:].
在这里,变量arg1将包含字符串'bar',arg2并将包含'baz'. 该对象sys.argv只是一个包含命令行中所有内容的列表。请注意,这sys.argv[0]是脚本的名称。例如,如果您只想要一个包含所有参数的列表,则可以使用sys.argv[1:].
回答by Mr_Spock
Just Taking Input
只是输入
the_input = raw_input("Enter input: ")
And that's it.
就是这样。
Moreover, if you want to make a list of inputs, you can do something like:
此外,如果要制作输入列表,可以执行以下操作:
a = []
for x in xrange(1,10):
a.append(raw_input("Enter Data: "))
In that case, you'll be asked for data 10 times to store 9 items in a list.
在这种情况下,您将被要求提供 10 次数据以在列表中存储 9 个项目。
Output:
输出:
Enter data: 2
Enter data: 3
Enter data: 4
Enter data: 5
Enter data: 7
Enter data: 3
Enter data: 8
Enter data: 22
Enter data: 5
>>> a
['2', '3', '4', '5', '7', '3', '8', '22', '5']
You can search that list the fundamental way with something like (after making that list):
您可以使用类似的基本方式搜索该列表(在制作该列表后):
if '2' in a:
print "Found"
else: print "Not found."
否则:打印“未找到”。
You can replace '2' with "raw_input()" like this:
您可以像这样用“raw_input()”替换“2”:
if raw_input("Search for: ") in a:
print "Found"
else:
print "Not found"
Taking Raw Data From Input File via Commandline Interface
通过命令行界面从输入文件中获取原始数据
If you want to take the input from a file you feed through commandline (which is normally what you need when doing code problems for competitions, like Google Code Jam or the ACM/IBM ICPC):
如果你想从你通过命令行提供的文件中获取输入(这通常是你在为比赛做代码问题时需要的,比如 Google Code Jam 或 ACM/IBM ICPC):
example.py
例子.py
while(True):
line = raw_input()
print "input data: %s" % line
In command line interface:
在命令行界面中:
example.py < input.txt
Hope that helps.
希望有帮助。
回答by Mike Helmick
If you're using Python 3, raw_inputhas changed to input
如果您使用的是 Python 3,raw_input则已更改为input
Python 3 example:
Python 3 示例:
line = input('Enter a sentence:')
回答by user8391127
Start your script with the following line. The script will first run and then you will get the python command prompt. At this point all variables and functions will be available for interactive use and invocations.
使用以下行启动您的脚本。该脚本将首先运行,然后您将获得 python 命令提示符。此时,所有变量和函数都可用于交互式使用和调用。
#!/usr/bin/env python -i
#!/usr/bin/env python -i

