3

I have problem with raw_input command. I use it in program and run the program in cmd. Then it asks input but when I paste there text which has empty lines in it, then python takes it automatically as enter command. I want it to understand that it's not enter command but just new line. How could I do this?

user2904150
  • 149
  • 1
  • 1
  • 9
  • 2
    It's hard to understand exactly what's causing your problem without seeing a bit of code. Can you include a short program (10 lines or so) that reproduces the problem? – Tim Pierce Dec 08 '13 at 15:25
  • First search gave me this: [how-to-process-raw-data-in-python](http://stackoverflow.com/questions/19202407/how-to-process-raw-data-in-python#answer-19202500) – Nagaraj Tantri Dec 08 '13 at 15:34

2 Answers2

2

To do that, you can't use raw_input function, because this function finish reading input after first new line character. Instead you can use sys.stdin:

>>>import sys
>>>input = sys.stdin.read()
hi
this text has
new lines
Ctrl^D
>>>print input
hi
this text has
new lines

To finish reading input you'll have to send EOF char, which in Linux is Ctrl+D and in Windows is Ctrl+Z

Hope this helps!

Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
1

as per python documentation, raw_input() is designed to have \n as the key that ends the input, and it has not been designed for another way of interacting.

If you want to use another way of using the input you can try using fileinput() or sys.stdin.read() and iterate over the lines or the characters and create the behavior you're looking for.

Have a look at the answers of questions such as that one

Community
  • 1
  • 1
zmo
  • 24,463
  • 4
  • 54
  • 90