如何从用户那里获取多行输入
How to get multiline input from user
我想编写一个获取多行输入并逐行处理的程序。为什么Python3中没有raw_input
这样的函数?
input
不允许用户用换行符分隔行 (Enter),它只打印第一行。
它可以存储在变量中,甚至可以读取到列表中吗?
在 Python 3.x 中 Python 2.x 的 raw_input()
已被 input()
函数取代。但是,在这两种情况下,您都不能输入多行字符串,为此,您需要逐行从用户那里获取输入,然后使用 \n
.join()
,或者您也可以采用多行并使用由 \n
分隔的 +
运算符连接它们
要从用户那里获取多行输入,您可以这样:
no_of_lines = 5
lines = ""
for i in xrange(no_of_lines):
lines+=input()+"\n"
print(lines)
或
lines = []
while True:
line = input()
if line:
lines.append(line)
else:
break
text = '\n'.join(lines)
使用 input()
内置函数获取用户的输入行。
您可以阅读the help here。
您可以使用以下代码一次获取多行(以空行结尾):
while input() != '':
do_thing
input(prompt)
基本上等同于
def input(prompt):
print(prompt, end='', file=sys.stderr, flush=True)
return sys.stdin.readline()
喜欢的可以直接阅读sys.stdin
lines = sys.stdin.readlines()
lines = [line for line in sys.stdin]
five_lines = list(itertools.islice(sys.stdin, 5))
前两个要求输入以某种方式结束,要么到达文件末尾,要么通过用户键入 Control-D(或 Windows 中的 Control-Z)来表示结束。最后一个将 return 在读取五行后,无论是从文件还是从 terminal/keyboard.
no_of_lines = 5
lines = ""
for i in xrange(5):
lines+=input()+"\n"
a=raw_input("if u want to continue (Y/n)")
""
if(a=='y'):
continue
else:
break
print lines
raw_input
可以正确处理 EOF,所以我们可以写一个循环,读取直到我们收到来自用户的 EOF (Ctrl-D):
Python 3
print("Enter/Paste your content. Ctrl-D or Ctrl-Z ( windows ) to save it.")
contents = []
while True:
try:
line = input()
except EOFError:
break
contents.append(line)
Python 2
print "Enter/Paste your content. Ctrl-D or Ctrl-Z ( windows ) to save it."
contents = []
while True:
try:
line = raw_input("")
except EOFError:
break
contents.append(line)
我想编写一个获取多行输入并逐行处理的程序。为什么Python3中没有raw_input
这样的函数?
input
不允许用户用换行符分隔行 (Enter),它只打印第一行。
它可以存储在变量中,甚至可以读取到列表中吗?
在 Python 3.x 中 Python 2.x 的 raw_input()
已被 input()
函数取代。但是,在这两种情况下,您都不能输入多行字符串,为此,您需要逐行从用户那里获取输入,然后使用 \n
.join()
,或者您也可以采用多行并使用由 \n
+
运算符连接它们
要从用户那里获取多行输入,您可以这样:
no_of_lines = 5
lines = ""
for i in xrange(no_of_lines):
lines+=input()+"\n"
print(lines)
或
lines = []
while True:
line = input()
if line:
lines.append(line)
else:
break
text = '\n'.join(lines)
使用 input()
内置函数获取用户的输入行。
您可以阅读the help here。
您可以使用以下代码一次获取多行(以空行结尾):
while input() != '':
do_thing
input(prompt)
基本上等同于
def input(prompt):
print(prompt, end='', file=sys.stderr, flush=True)
return sys.stdin.readline()
喜欢的可以直接阅读sys.stdin
lines = sys.stdin.readlines()
lines = [line for line in sys.stdin]
five_lines = list(itertools.islice(sys.stdin, 5))
前两个要求输入以某种方式结束,要么到达文件末尾,要么通过用户键入 Control-D(或 Windows 中的 Control-Z)来表示结束。最后一个将 return 在读取五行后,无论是从文件还是从 terminal/keyboard.
no_of_lines = 5
lines = ""
for i in xrange(5):
lines+=input()+"\n"
a=raw_input("if u want to continue (Y/n)")
""
if(a=='y'):
continue
else:
break
print lines
raw_input
可以正确处理 EOF,所以我们可以写一个循环,读取直到我们收到来自用户的 EOF (Ctrl-D):
Python 3
print("Enter/Paste your content. Ctrl-D or Ctrl-Z ( windows ) to save it.")
contents = []
while True:
try:
line = input()
except EOFError:
break
contents.append(line)
Python 2
print "Enter/Paste your content. Ctrl-D or Ctrl-Z ( windows ) to save it."
contents = []
while True:
try:
line = raw_input("")
except EOFError:
break
contents.append(line)