multiline raw input

Bengt Richter bokr at oz.net
Fri Mar 15 19:18:47 EST 2002


On 15 Mar 2002 15:06:50 -0800, les_ander at yahoo.com (les ander) wrote:

>Hi,
>i have a program that takes input from the command line
>however this input is multi-line and will have a lot of backslashes
>I tried using sys.stdin but it ignores the backslashes.
>i tried with raw_input but that only takes in the first line
>
>can some suggest a remedy?
>
>
>
>
>for example: 
>i would like to run my program as follows:
>
>>> myprog.py 
>Type in some input (type in '.' to end) :
>\sum_{i=1}^{n} \frac{\sqrt{2}}{\alpha} \infty
>\begin{itemize}
>\item this equation says that 
>\end{itemize}
>.
>
>
 >>> def rawlines(prompt, lineprompt=''):
 ...     lines = [raw_input(prompt+'\n'+lineprompt)]
 ...     while lines[-1:][:]!=['.']:
 ...         lines.append(raw_input(lineprompt))
 ...     return lines
 ...
 >>> lines = rawlines("Type in some input (type in '.' to end) :","=>")
 Type in some input (type in '.' to end) :
 =>\sum_{i=1}^{n} \frac{\sqrt{2}}{\alpha} \infty
 =>\begin{itemize}
 =>\item this equation says that
 =>\end{itemize}
 =>.
 >>> for line in lines: print line
 ...
 \sum_{i=1}^{n} \frac{\sqrt{2}}{\alpha} \infty
 \begin{itemize}
 \item this equation says that
 \end{itemize}
 .

Return lines[:-1] if you don't want the ending dot line back.

I passed a line prompt of '=>', but you can let it default to ''
to get what you specified. OTOH, you might want to consider
something other than '' as the line prompt, to make it clear
that it's waiting for input.

Regards,
Bengt Richter





More information about the Python-list mailing list