6

Is there any easy way to handle multiple lines user input in command-line Python application?

I was looking for an answer without any result, because I don't want to:

  • read data from a file (I know, it's the easiest way);
  • create any GUI (let's stay with just a command line, OK?);
  • load text line by line (it should pasted at once, not typed and not pasted line by line);
  • work with each of lines separately (I'd like to have whole text as a string).

What I would like to achieve is to allow user pasting whole text (containing multiple lines) and capture the input as one string in entirely command-line tool. Is it possible in Python?

It would be great, if the solution worked both in Linux and Windows environments (I've heard that e.g. some solutions may cause problems due to the way cmd.exe works).

kurp
  • 231
  • 1
  • 5
  • 12
  • 1
    I don't think the Windows command prompt deals with multiple lines like you want it to. And what is the purpose of this? Maybe if you give some context someone might have a different approach to the problem. – Aesthete Sep 19 '12 at 11:40
  • @Aesthete, do you know perhaps how about Linux? If it works in Linux it may be even enough for me... Regarding the context - it's just for a small tool making some annoying tasks for me (gets a few strings and produces text output). At the next stage it'll just load and parse indicated webpage directly; for now I'm looking for the simplest way to input data (task isn't repeatable, so I consider fighting with files or raw_input-loops as a bit redundant, since I'd like to just paste one string at once and that's all:). I'd like to use the method (if exists) for some other tools too... – kurp Sep 19 '12 at 11:54
  • you said you have "lots of text you want to copy and paste into a program" where does this text come from? surely its in a text document, a webpage, or some other type of already-digitally saved format, why not simply read from it? if your purpose is efficiencty/timesaving/automation why should the user ctrl-c ctrl-v the content? python supports passing data with subprocess, so you can write your program to support copy-paste and another to send it lots of data. since you mention you want to expand later anyway. – Inbar Rose Sep 19 '12 at 12:18
  • @InbarRose, thank you for your answer. Well, IMO copy-pasting actually IS the simplest and efficient method in this case. I'd like to input a part of webpage. As I said, connecting webpage and parsing it to extract what I'm looking for will be probably the next stage (and needs some work to build downloading-and-parsing mechanism), but for now I'm OK with copy-paste, which would be simple and easy, I believe. "Why not simply read from it?" you've asked. Pasting into text file and saving on HDD to read it from app seems to be less comfortable solution... – kurp Sep 19 '12 at 12:37

3 Answers3

7
import sys

text = sys.stdin.read()

After pasting, you have to tell python that there is no more input by sending an end-of-file control character (ctrl+D in Linux, ctrl+Z followed by enter in Windows).

This method also works with pipes. If the above script is called paste.py, you can do

$ echo "hello" | python paste.py

and text will be equal to "hello\n". It's the same in windows:

C:\Python27>dir | python paste.py

The above command will save the output of dir to the text variable. There is no need to manually type an end-of-file character when the input is provided using pipes -- python will be notified automatically when the program creating the input has completed.

Lauritz V. Thaulow
  • 49,139
  • 12
  • 73
  • 92
  • Wow, two-line solution - look like more than I've expected :D I'm not able to check this at the moment, but thank you for the answer. I'll provide my feedback in a few hours. – kurp Sep 19 '12 at 13:02
2

You could get the text from clipboard without any additional actions which raw_input() requires from a user to paste the multiline text:

import Tkinter
root = Tkinter.Tk()
root.withdraw()

text = root.clipboard_get()

root.destroy()

See also How do I copy a string to the clipboard on Windows using Python?

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Thank you for the suggestion! I haven't even consider such approach, but it's really great alternative. – kurp Sep 20 '12 at 22:13
0

Use :

input = raw_input("Enter text")

These gets in input as a string all the input. So if you paste a whole text, all of it will be in the input variable.

EDIT: Apparently, this works only with Python Shell on Windows.

coredump
  • 3,017
  • 6
  • 35
  • 53
  • 2
    Not if the text has newlines in it ... Only the portion up to the newline will be read – mgilson Sep 19 '12 at 11:57
  • @mgilson I know that says in the documentation. But, apparently, when you copy paste it works. I copy pasted a whole page of text with plenty of newlines. It works. – coredump Sep 19 '12 at 12:06
  • 1
    I copy-pasted a small piece of code with 1 newline in it, and it didn't work. Maybe it's terminal dependent? – mgilson Sep 19 '12 at 12:07
  • @mgilson I used the Python Shell for version 2.7 on Windows. – coredump Sep 19 '12 at 12:09
  • I'm on OS-X using `terminal` which is the default commandline interface ... (I suppose I could test it on Ubuntu linux too ...) – mgilson Sep 19 '12 at 12:11
  • @mgilson Ok. So the Python oficial shell does not conform with it's own specifications :) . – coredump Sep 19 '12 at 12:21