paste text with newlines into raw_input?

B

BartlebyScrivener

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
 
D

Daniel Gee

#!/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 "==============="
 
H

half.italian

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
 
B

BartlebyScrivener

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
 
B

BartlebyScrivener

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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top