如何读取Python中的多行原始input?
我想创build一个需要多行用户input的Python程序。 例如:
This is a multilined input. It has multiple sentences. Each sentence is on a newline.
我怎样才能接受多行的原始input?
sentinel = '' # ends when this string is seen for line in iter(raw_input, sentinel): pass # do things here
要把每一行都作为一个string,你可以这样做:
'\n'.join(iter(raw_input, sentinel))
Python 3:
'\n'.join(iter(input, sentinel))
继续阅读线路,直到用户input一个空行(或更改其他stopword
)
text = "" stopword = "" while True: line = raw_input() if line.strip() == stopword: break text += "%s\n" % line print text
或者,您可以尝试sys.stdin.read()
import sys s=sys.stdin.read() print s