Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get multiline input from user [duplicate]

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_input in Python 3?

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

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

like image 477
MaciejPL Avatar asked May 14 '15 13:05

MaciejPL


People also ask

How do you read multiple lines in Python?

To read multiple lines, call readline() multiple times. The built-in readline() method return one line at a time. To read multiple lines, call readline() multiple times.


1 Answers

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

Python 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

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) 
like image 138
xiaket Avatar answered Oct 06 '22 01:10

xiaket