By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,758 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

paste text with newlines into raw_input?

P: n/a
Using Python on Debian Etch.

What is the best way to paste a block of text in at the command
prompt.

I'm trying something like:

Quote = raw_input("Paste quote here: ")

Which works great for one line of text with a single newline. It gets
stripped. Okay.

Is there a way to paste in a block of text that has multiple lines and
newlines? I don't care if they all get stripped in the process, in
fact I'd prefer it. I've used strip before, but that doesn't seem to
work until you get the text into the program.

Thanks for any help.

Rick

May 30 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
#!/usr/bin/python

print "paste quote:"
emptycount = 0
lines = []

while emptycount < 2:
t = raw_input()
if len(t) == 0:
emptycount +=1
else:
emptycount=0
lines.append(t)
lines.append("\n")

quote = " ".join(lines[:-3])

print "Quote was this:"
print "==============="
print quote
print "==============="

May 31 '07 #2

P: n/a
On May 30, 2:04 pm, BartlebyScrivener <bscrivene...@gmail.comwrote:
Using Python on Debian Etch.

What is the best way to paste a block of text in at the command
prompt.

I'm trying something like:

Quote = raw_input("Paste quote here: ")

Which works great for one line of text with a single newline. It gets
stripped. Okay.

Is there a way to paste in a block of text that has multiple lines and
newlines? I don't care if they all get stripped in the process, in
fact I'd prefer it. I've used strip before, but that doesn't seem to
work until you get the text into the program.

Thanks for any help.

Rick
import sys
s =sys.stdin.read()
print s

which will read until ctrl-d

~Sean

May 31 '07 #3

P: n/a
Thanks,

I think I need a Tkinter text entry widget, but it will take me a week
to learn how to set it up.

I'll report back.

rick

May 31 '07 #4

P: n/a
Hi,

I'm going to post this here in case somebody else searches for an
example Tkinter Text Widget for entering multiline text. I don't like
GUI and don't even quite understand how it works, but it seems to
work. In my case it's part of a program for pasting a quote from the
clipboard into a MySQL database (hence the separate paste button).

I also don't know OO and Classes. If someone wants to wrap it in a
class and repost it could save the free world.

Or suggestions for making it better much appreciated.

Thanks,

Rick

---------------------------------
#! /usr/bin/python
import Tkinter
import tkFont
"""
Tkinter Text Widget for entering multline text.
Rick Dooling http://dooling.com

Based on:

Alan Gauld's "GUI Programming with Tkinter"
http://www.freenetpages.co.uk/hp/alan.gauld/tutgui.htm

Jeff Eppler's clp post - 3 August 2005
"cut and paste text between Tkinter widgets"
http://tinyurl.com/2d97gj
"""

# the first two functions come from Jeff Eppler's post
def make_menu(w):
global the_menu
the_menu = Tkinter.Menu(w, tearoff=0)
the_menu.add_command(label="Cut")
the_menu.add_command(label="Copy")
the_menu.add_command(label="Paste")

def show_menu(e):
w = e.widget
the_menu.entryconfigure("Cut",
command=lambda: w.event_generate("<<Cut>>"))
the_menu.entryconfigure("Copy",
command=lambda: w.event_generate("<<Copy>>"))
the_menu.entryconfigure("Paste",
command=lambda: w.event_generate("<<Paste>>"))
the_menu.tk.call("tk_popup", the_menu, e.x_root, e.y_root)

def evClear():
eText.delete(0.0,Tkinter.END)

def assign():
# get text from the text widget and assign it to Quote
Quote = eText.get(0.0, Tkinter.END)
# just for testing the assignment
print Quote

def paste():
eText.event_generate("<<Paste>>")

t = Tkinter.Tk()

# create the top level window/frame
F = Tkinter.Frame(t)
F.master.title("Enter Quote ")
F.pack(expand="true")

myfont = tkFont.Font(family="Courier", size=14)

# frame for message to the troops
fMessage = Tkinter.Frame(F, border=1)
fMessage.pack(side="top", expand="true")
lMessage = Tkinter.Label(fMessage, text="Paste your quote into the
Text Box from the clipboard, or type it in. When you are finished,
click Enter.")
lMessage.pack(expand="true")

# frame for text entry field
fText = Tkinter.Frame(F, border=1)
fText.pack(side="top", expand="true")
# the text widget
eText = Tkinter.Text(fText, width= 75, height=20, font=myfont,
wrap=Tkinter.WORD); eText.pack(side="top")
eText.bind_class("Text", "<Button-3><ButtonRelease-3>", show_menu)

# frame with the buttons
fButtons = Tkinter.Frame(F, relief="groove", border=3)
# the buttons
bPaste = Tkinter.Button(fButtons, text="Paste", command=paste)
bPaste.pack(side="left", padx=15, pady=4)
bEnter = Tkinter.Button(fButtons, text="Enter", command=assign)
bEnter.pack(side="left", padx=15, pady=4)
bClear = Tkinter.Button(fButtons, text="Clear Text", command=evClear)
bClear.pack(side="left", padx=15, pady=4)
bQuit = Tkinter.Button(fButtons, text="Quit", command=F.quit)
bQuit.pack(side="left", padx=15, pady=4)
# pack them
fButtons.pack(side="bottom", expand="true")

make_menu(t)

t.mainloop()


May 31 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.