Python 获取用户输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3345202/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:33:34  来源:igfitidea点击:

Getting user input

pythonuser-input

提问by l--''''''---------''''''''''''

I am running this:

我正在运行这个:

import csv
import sys
reader = csv.reader(open(sys.argv[0], "rb"))
for row in reader:
    print row

And I get this in response:

我得到这个回应:

['import csv']
['import sys']
['reader = csv.reader(open(sys.argv[0]', ' "rb"))']
['for row in reader:']
['    print row']
>>> 

For the sys.argv[0]I would like it to prompt me to enter a filename.

对于sys.argv[0]我希望它提示我输入文件名。

How do I get it to prompt me to enter a filename?

如何让它提示我输入文件名?

采纳答案by Sunny Tambi

In python 3.x, use input()instead of raw_input()

在 python 3.x 中,使用input()代替raw_input()

回答by Dave Webb

Use the raw_input()functionto get input from users (2.x):

使用该raw_input()函数从用户(2.x)获取输入:

print "Enter a file name:",
filename = raw_input()

or just:

要不就:

filename = raw_input('Enter a file name: ')

or if in Python 3.x:

或者如果在 Python 3.x 中:

filename = input('Enter a file name: ')

回答by Nikolaus Gradwohl

sys.argv[0]is not the first argument but the filename of the python program you are currently executing. I think you want sys.argv[1]

sys.argv[0]不是第一个参数,而是您当前正在执行的 python 程序的文件名。我想你想要sys.argv[1]

回答by Kyle Falconer

To supplement the above answers into something a little more re-usable, I've come up with this, which continues to prompt the user if the input is considered invalid.

为了将上述答案补充为更可重用的内容,我想出了这个,如果输入被认为无效,它会继续提示用户。

try:
    input = raw_input
except NameError:
    pass

def prompt(message, errormessage, isvalid):
    """Prompt for input given a message and return that value after verifying the input.

    Keyword arguments:
    message -- the message to display when asking the user for the value
    errormessage -- the message to display when the value fails validation
    isvalid -- a function that returns True if the value given by the user is valid
    """
    res = None
    while res is None:
        res = input(str(message)+': ')
        if not isvalid(res):
            print str(errormessage)
            res = None
    return res

It can be used like this, with validation functions:

它可以像这样使用,带有验证功能:

import re
import os.path

api_key = prompt(
        message = "Enter the API key to use for uploading", 
        errormessage= "A valid API key must be provided. This key can be found in your user profile",
        isvalid = lambda v : re.search(r"(([^-])+-){4}[^-]+", v))

filename = prompt(
        message = "Enter the path of the file to upload", 
        errormessage= "The file path you provided does not exist",
        isvalid = lambda v : os.path.isfile(v))

dataset_name = prompt(
        message = "Enter the name of the dataset you want to create", 
        errormessage= "The dataset must be named",
        isvalid = lambda v : len(v) > 0)

回答by Ezra A.Mosomi

Use the following simple way to interactively get user data by a prompt as Arguments on what you want.

使用以下简单方法通过提示以交互方式获取用户数据作为您想要的参数。

Version : Python 3.X

版本:Python 3.X

name = input('Enter Your Name: ')
print('Hello ', name)