This is a multilined input.
It has multiple sentences.
Each sentence is on a newline.
我该如何输入多行原始输入?
This is a multilined input.
It has multiple sentences.
Each sentence is on a newline.
我该如何输入多行原始输入?
sentinel = '' # ends when this string is seen
for line in iter(input, sentinel):
pass # do things here
'\n'.join(iter(input, sentinel))
Python 2:
'\n'.join(iter(raw_input, sentinel))
iter()
的这种其他形式。您真是一个天才! - inspectorG4dgetiter(lambda: raw_input('prompt'), sentinel)
- jamylakraw_input
现在是input
。 - wecsam或者你可以尝试使用sys.stdin.read()
,它会返回整个输入直到出现EOF
:
import sys
s = sys.stdin.read()
print(s)
当用户输入空行(或更改stopword
为其他内容)时,继续读取行。
text = ""
stopword = ""
while True:
line = raw_input()
if line.strip() == stopword:
break
text += "%s\n" % line
print text
对于这个答案的扩展 https://dev59.com/P2gu5IYBdhLWcg3wASWO#11664652,你可以检查一行是否存在,而不是任何停用词。
content = []
while True:
line = raw_input()
if line:
content.append(line)
else:
break
print '\n'.join(content)
试试这个
import sys
lines = sys.stdin.read().splitlines()
print(lines)
输入:
1
2
3
4
输出: ['1', '2', '3', '4']
我自己也曾经为这个问题苦苦挣扎了很长时间,因为我想找到一种方法来读取多行用户输入而不需要用户用Control D(或停止词)来终止它。最终我在Python3中找到了一种方法,使用pyperclip模块(您需要使用pip install安装)。以下是一个示例,它接受IP列表。
import pyperclip
lines = 0
while True:
lines = lines + 1 #counts iterations of the while loop.
text = pyperclip.paste()
linecount = text.count('\n')+1 #counts lines in clipboard content.
if lines <= linecount: # aslong as the while loop hasn't iterated as many times as there are lines in the clipboard.
ipaddress = input()
print(ipaddress)
else:
break
对我来说,这正是我所需要的;接收多行输入,执行必要的操作(这里是简单的打印),并在处理完最后一行后跳出循环。希望它对你也同样有用。
注:Original Answer翻译成“最初的回答”使用Python 3,您可以将每行分配给data
:
while data := input():
print("line", data)
当你知道你想让Python读取的确切行数时,从提示/控制台中读取多行的最简单方法是使用列表推导式。
lists = [ input() for i in range(2)]
上面的代码读取2行。并将输入保存在列表中。
sys.stdin.read() 可以用于获取用户的多行输入。例如:
>>> import sys
>>> data = sys.stdin.read()
line one
line two
line three
<<Ctrl+d>>
>>> for line in data.split(sep='\n'):
print(line)
o/p:line one
line two
line three
#!/usr/bin/env python
from prompt_toolkit import prompt
from prompt_toolkit.formatted_text import HTML
def prompt_continuation(width, line_number, wrap_count):
"""
The continuation: display line numbers and '->' before soft wraps.
Notice that we can return any kind of formatted text from here.
The prompt continuation doesn't have to be the same width as the prompt
which is displayed before the first line, but in this example we choose to
align them. The `width` input that we receive here represents the width of
the prompt.
"""
if wrap_count > 0:
return " " * (width - 3) + "-> "
else:
text = ("- %i - " % (line_number + 1)).rjust(width)
return HTML("<strong>%s</strong>") % text
if __name__ == "__main__":
print("Press [Meta+Enter] or [Esc] followed by [Enter] to accept input.")
answer = prompt(
"Multiline input: ", multiline=True, prompt_continuation=prompt_continuation
)
print("You said: %s" % answer)
使用此代码,您可以获得多行输入,即使在输入后续行之后,每行也可以进行编辑。还有一些不错的附加功能,例如行号。输入以按下 escape 键然后按下 enter 键结束:
~/Desktop ❯ py prompt.py
按 [Meta+Enter] 或 [Esc] 然后按 [Enter] 接受输入。
多行输入:第一行文本,然后按 enter
- 2 - 第二行文本,然后按 enter
- 3 - 第三行文本,箭头键可用于移动,按 enter
- 4 - 并且可以根据需要编辑行,直到您
- 5 - 按下 escape 键然后按下 enter 键
您说:第一行文本,然后按 enter
第二行文本,然后按 enter
第三行文本,箭头键可用于移动,按 enter
并且可以根据需要编辑行,直到您
按下 escape 键然后按下 enter 键
~/Desktop ❯
raw_input
的解决方案还是直接从stdin
获取输入也可以? - jamylak