Python 如何从用户获取多行输入

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

How to get multiline input from user

pythoninputmultiline

提问by MaciejPL

I want to write a program that gets multiple line input and work with it line by line. Why there is no function like raw_inputin Python 3?

我想编写一个获取多行输入并逐行处理的程序。为什么没有像raw_inputPython 3这样的函数?

inputdoes not allow user to put lines separated by newline (Enter), it prints back only the first line.

input不允许用户放置由换行符 ( Enter)分隔的行,它只打印第一行。

Can it be stored in variable or even read it to a list?

它可以存储在变量中,甚至可以读取到列表中吗?

采纳答案by ZdaR

In Python 3.x the raw_input()of Python 2.x has been replaced by input()function. However in both the cases you cannot input multi-line strings, for that purpose you would need to get input from the user line by line and then .join()them using \n, or you can also take various lines and concatenate them using +operator separated by \n

在 Python 3.x 中raw_input(),Python 2.x 的input()功能已被函数取代。然而,在这两种情况下,你不能输入多行字符串,为此你需要从线用户线得到输入,然后.join()他们使用\n,或者你也可以采取各种线条和使用它们串联+操作相隔\n

To get multi-line input from the user you can go like:

要从用户那里获得多行输入,您可以这样做:

no_of_lines = 5
lines = ""
for i in xrange(no_of_lines):
    lines+=input()+"\n"

print(lines)

Or

或者

lines = []
while True:
    line = input()
    if line:
        lines.append(line)
    else:
        break
text = '\n'.join(lines)

回答by maggick

Use the input()built-in function to get a input line from the user.

使用input()内置函数从用户那里获取输入行。

You can read the help here.

您可以在此处阅读帮助

You can use the following code to get several line at once (finishing by an empty one):

您可以使用以下代码一次获取多行(以空行结束):

while input() != '':
    do_thing

回答by chepner

input(prompt)is basically equivalent to

input(prompt)基本上相当于

def input(prompt):
    print(prompt, end='', file=sys.stderr)
    return sys.stdin.readline()

You can read directly from sys.stdinif you like.

sys.stdin如果您愿意,可以直接阅读。

lines = sys.stdin.readlines()

lines = [line for line in sys.stdin]

five_lines = list(itertools.islice(sys.stdin, 5))

The first two require that the input end somehow, either by reaching the end of a file or by the user typing Control-D (or Control-Z in Windows) to signal the end. The last one will return after five lines have been read, whether from a file or from the terminal/keyboard.

前两个要求输入以某种方式结束,要么到达文件末尾,要么用户键入 Control-D(或 Windows 中的 Control-Z)以表示结束。无论是从文件还是从终端/键盘读取五行后,最后一行都会返回。

回答by mohankumar.A

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

回答by xiaket

raw_inputcan correctly handle the EOF, so we can write a loop, read till we have received an EOF (Ctrl-D) from user:

raw_input可以正确处理 EOF,因此我们可以编写一个循环,读取直到我们收到用户的 EOF (Ctrl-D):

Python 3

蟒蛇 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

蟒蛇 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)