Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python command Line - multiple Line Input

I'm trying to solve a Krypto Problem on https://www.spoj.pl in Python, which involves console input.

My Problem is, that the Input String has multiple Lines but is needed as one single String in the Programm. If I just use raw_input() and paste (for testing) the text in the console, Python threats it like I pressed enter after every Line -> I need to call raw_input() multiple times in a loop.

The Problem is, that I cannot modify the Input String in any way, it doesn't have any Symbol thats marks the End and I don't know how many Lines there are.

So what do I do?

like image 814
Dreiven Avatar asked May 01 '11 18:05

Dreiven


2 Answers

Upon reaching end of stream on input, raw_input will return an empty string. So if you really need to accumulate entire input (which you probably should be avoiding given SPOJ constraints), then do:

buffer = ''
while True:
    line = raw_input()
    if not line: break

    buffer += line

# process input
like image 159
Cat Plus Plus Avatar answered Sep 22 '22 09:09

Cat Plus Plus


Since the end-of-line on Windows is marked as '\r\n' or '\n' on Unix system it is straight forward to replace those strings using

your_input.replace('\r\n', '')

like image 24
Andreas Jung Avatar answered Sep 22 '22 09:09

Andreas Jung