Thread: Python?

  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Rep Power



    I want to create a simple copy/past program that will read a line of plain text, remove pre-defined words or unwanted text and then reformat the remaining text. Is this something that could easily achieved with Python?

    I have no previous programming experience so I going to try and self -teach, I was just wondering if Python was a good option?

  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    It's fairly easy in python. Run this code as an example. For a real application I'd want to be more careful about punctuation and capital letters to start sentences and two spaces or more separating sentences. Finding words was easy, removing some words was easy, and reformatting was easy.
    import textwrap
    #Madame Bovary
    text = '''As she was a long time before she found
    her work-case, her father grew impatient; she did not
    answer, but as she sewed she pricked her fingers, which
    she then put to her mouth to suck them. Charles was
    surprised at the whiteness of her nails. They were shiny,
    delicate at the tips, more polished than the ivory of
    Dieppe, and almond-shaped. Yet her hand was not
    beautiful, perhaps not white enough, and a little hard at
    the knuckles; besides, it was too long, with no soft
    inflections in the outlines. Her real beauty was in her eyes.'''
    def filtered(s):
        ''' remove 3 letter "words" '''
        words = s.split()
        return ' '.join(word for word in words if len(word) != 3)
    print(textwrap.fill(text, width=20))
    print(textwrap.fill(filtered(text), width=20))
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo