Python 3: get user input, including newlines

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

lcbeika rraobmlo grmfina ontccep emrlin tseiboo edosrgd mkoeys eissaml knaiefr 

Using input , I can only read the first word as soon as it reads the first new line, which it stops reading.

Is there any way that I could read everything in them without iteratively calling input ?

+6
python input
source share
2 answers

You can import sys and use 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. 
+10
source share

if you pass text to 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 
-one
source share

All Articles