473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

paste text with newlines into raw_input?

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
4 5411
#!/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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
1
by: Sam Quigley | last post by:
Hi, I want to use XSL to wrap paragraphs of text in <p> tags automatically, basically using the heuristic that two chunks of text separated by 2 consecutive newlines are to be treated as two...
1
by: wtnt | last post by:
Hello. I've searched all over and haven't seen another thread with this problem. Please bear with me as I try to explain. thanks. :) I have some programs that need to be cross-platform...
5
by: spectre | last post by:
Hi, I need to copy/paste texts form word, notepad... to an html textarea to store it into a database. With the <pre>tag, and a text with long lines, the text displayed in the textarea needs...
13
by: sonald | last post by:
Hi, Can anybody tell me how to change the text delimiter in FastCSV Parser ? By default the text delimiter is double quotes(") I want to change it to anything else... say a pipe (|).. can anyone...
10
by: Steve | last post by:
Hi, I've created a Python program that a user enteres one line of text which will then create an acronym from that text. What I want to add to the program is the abilty to rerun this process...
7
by: Collin | last post by:
I'm pretty new to Python, but this has really bugged me. I can't find a way around it. The problem is that, when I use raw_input("sajfasjdf") whatever, or input("dsjfadsjfa"), you can only...
1
by: nick777 | last post by:
Hope the Community can bear with me as I muddle with the vocabulary since I am not really sure if I am going about this the correct way. My question is as follows: If I had some sample data in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.