Python command line - multiple line inputs

I am trying to solve a Krypto problem on https://www.spoj.pl in Python, which includes console input.

My problem is that the input line has several lines, but is needed as one line in the program. If I just use raw_input () and paste (for testing) the text in the console, Python threatens it, as I clicked, type after each line -> I need to call raw_input () several times in a loop.

The problem is that I can’t change the input line in any way, it doesn’t have a character that marks the end, and I don’t know how many lines there are.

So what should I do?

+6
source share
4 answers

Upon reaching the end of the stream at the input, raw_input will return an empty string. Therefore, if you really need to accumulate all the input (which probably you should avoid the SPOJ constraints given), follow these steps:

buffer = '' while True: line = raw_input() if not line: break buffer += line # process input 
+6
source

Since the end of the line in Windows is marked as \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

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

+1
source

Since raw_input() intended to read a single line, you may have problems this way. A simple solution would be to put the input string in a text file and parse from there.

Assuming you have input.txt , you can take values ​​as

 f = open(r'input.txt','rU') for line in f: print line, 
+1
source

Using the best answer here, you will still have an EOF error that needs to be handled. So, I just added exception handling here

 buffer = '' while True: try: line = raw_input() except EOFError: break if not line: break buffer += line 
0
source

Source: https://habr.com/ru/post/887064/


All Articles