paste text with newlines into raw_input?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BartlebyScrivener

    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("Past e 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

  • Daniel Gee

    #2
    Re: paste text with newlines into raw_input?

    #!/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(line s[:-3])

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

    Comment

    • half.italian@gmail.com

      #3
      Re: paste text with newlines into raw_input?

      On May 30, 2:04 pm, BartlebyScriven er <bscrivene...@g mail.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("Past e 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

      Comment

      • BartlebyScrivener

        #4
        Re: paste text with newlines into raw_input?

        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

        Comment

        • BartlebyScrivener

          #5
          Re: paste text with newlines into raw_input?

          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"


          Jeff Eppler's clp post - 3 August 2005
          "cut and paste text between Tkinter widgets"

          """

          # 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_co mmand(label="Cu t")
          the_menu.add_co mmand(label="Co py")
          the_menu.add_co mmand(label="Pa ste")

          def show_menu(e):
          w = e.widget
          the_menu.entryc onfigure("Cut",
          command=lambda: w.event_generat e("<<Cut>>"))
          the_menu.entryc onfigure("Copy" ,
          command=lambda: w.event_generat e("<<Copy>>") )
          the_menu.entryc onfigure("Paste ",
          command=lambda: w.event_generat e("<<Paste>>" ))
          the_menu.tk.cal l("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_gen erate("<<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(fam ily="Courier", size=14)

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

          # frame for text entry field
          fText = Tkinter.Frame(F , border=1)
          fText.pack(side ="top", expand="true")
          # the text widget
          eText = Tkinter.Text(fT ext, width= 75, height=20, font=myfont,
          wrap=Tkinter.WO RD); eText.pack(side ="top")
          eText.bind_clas s("Text", "<Button-3><ButtonReleas e-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(sid e="left", padx=15, pady=4)
          bEnter = Tkinter.Button( fButtons, text="Enter", command=assign)
          bEnter.pack(sid e="left", padx=15, pady=4)
          bClear = Tkinter.Button( fButtons, text="Clear Text", command=evClear )
          bClear.pack(sid e="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(s ide="bottom", expand="true")

          make_menu(t)

          t.mainloop()




          Comment

          Working...