开发者

Python 3: receive user input including newline characters

I'm trying to read in the following text from the command-line in Python 3 (copied verbatim, newlines and all):

lcbeika
rraobmlo
grmfina
ontccep
emrlin
tseiboo
ed开发者_如何学Goosrgd
mkoeys
eissaml
knaiefr

Using input, I can only read in the first word as once it reads the first newline it stops reading.

Is there a way I could read in them all without iteratively calling input?


You can import sys and use the methods on sys.stdin for example:

text = sys.stdin.read()

or:

lines = sys.stdin.readlines()

or:

for line in sys.stdin:
    # Do something with line.


if you are passing the text into your script as a file , you can use readlines()

eg

data=open("file").readlines()

or you can use fileinput

import fileinput
for line in fileinput.input():
    print line
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜