2010-07-27 137 views
118

我正在此:獲取用戶輸入

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

而且我得到這樣的迴應:

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

對於sys.argv[0]我想它提示我輸入一個文件名。

我該如何得到它以提示我輸入文件名?

+0

你想要的文件的名稱來自於用戶輸入或命令行參數? (例如python myScript.py inputfile.txt) – 2010-07-27 15:28:21

+2

由於您剛剛開始使用Python,因此查看教程並學習該語言的基礎知識可能是一個好主意,而不是嘗試僅學習您需要的功能,當你找不到東西時,在StackOverflow上搜索答案。這需要更多的時間,但是你會更好地理解語言。 – chimeracoder 2010-07-27 15:39:58

+8

@chimeracoder:理所當然,他走的很簡單,但正是這些問題讓我能夠快速找到答案,如果我只是在谷歌上查找它。對於一個小型項目而言,並沒有太多時間python是選擇的工具,因爲它很簡單,所以不必閱讀整個教程。 – 2013-06-09 19:48:29

回答

81

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

+3

自3.x以來,input()優於raw_input()是否有特定的原因?除了更容易輸入... – 2016-04-05 12:25:22

+41

@SilverRingvee'raw_input'不存在。 – wizzwizz4 2016-06-08 13:50:35

+31

一個不錯的理由。 – 2017-07-27 15:52:36

187

使用raw_input() function來獲取用戶輸入(2.X):

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

或者只是:

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

,或者在Python 3.X:

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

或'raw_input(「輸入文件名:」)' – 2010-07-27 15:31:07

+0

@Longpoke - 好點;已經更新了答案。 – 2010-07-27 15:31:43

+44

在Python 3中,它只是'input()' - __caution__:在2.x中也有一個輸入方法,但它是eval()的輸入,因此是__evil__。 – delnan 2010-07-27 16:21:08

26

sys.argv[0]不是第一個參數,而是當前正在執行的python程序的文件名。我想你想sys.argv[1]

+3

不是他所要求的,但有用的都不少 – Sirens 2013-04-22 23:20:19

7

爲上述答案補充到的東西多一點的可重複使用的,我想出了這一點,這繼續提示用戶是否認爲輸入無效。

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 

它可以像這樣使用,以驗證功能:

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) 
+0

...沒有真正回答這個問題...... – wizzwizz4 2016-06-08 13:52:05

+0

@ wizzwizz4它絕對會。你讀過我放的東西嗎? – 2016-06-08 13:53:28

+0

它污染了全局命名空間(在Python 2中覆蓋輸入),它看起來真的很難使用(什麼是「self」?)。我已經upvoted,但它需要在文檔上的一些工作。 – wizzwizz4 2016-06-08 13:59:15

5

使用以下簡單的方法來交互 獲得用戶一個提示爲你想要什麼樣的參數數據。

版本:的Python 3.X

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