Pobieranie danych użytkownika [duplikat]
To pytanie ma już odpowiedź tutaj:
- Jak odczytać wejście z klawiatury? 4 odpowiedzi
Prowadzę to:
import csv
import sys
reader = csv.reader(open(sys.argv[0], "rb"))
for row in reader:
print row
I dostaję to w odpowiedzi:
['import csv']
['import sys']
['reader = csv.reader(open(sys.argv[0]', ' "rb"))']
['for row in reader:']
[' print row']
>>>
Dla sys.argv[0]
chciałbym, aby monitował mnie o podanie nazwy pliku.
Jak sprawić, by monitował mnie o podanie nazwy pliku?
5 answers
W Pythonie 3.x, Użyj input()
zamiast raw_input()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-20 06:04:21
Użyj raw_input()
function to get input from users (2.x):
print "Enter a file name:",
filename = raw_input()
Lub po prostu:
filename = raw_input('Enter a file name: ')
Lub jeśli w Pythonie 3.x:
filename = input('Enter a file name: ')
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-31 04:37:28
sys.argv[0]
nie jest pierwszym argumentem, ale nazwą pliku aktualnie wykonywanego programu Pythona. Myślę, że chcesz sys.argv[1]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-27 15:32:50
Aby uzupełnić powyższe odpowiedzi w coś bardziej nadającego się do ponownego użycia, wymyśliłem to, co nadal wyświetla monit użytkownika, jeśli Dane wejściowe są uważane za nieprawidłowe.
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
Może być używany w ten sposób, z funkcjami walidacji:
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)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-08 13:55:36
Użyj następującego prostego sposobu, aby interaktywnie uzyskaj dane Użytkownika za pomocą zachęty jako argumenty na to, co chcesz.
Version: Python 3.X
name = input('Enter Your Name: ')
print('Hello ', name)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-28 17:51:45