How to get only words from the string using python

python-2.7, string

Solution

Probably the simplest way to do this is a simple loop testing against `string.ascii_letters` plus a specific subset of extra characters (e.g., `'-`):

>>> import string
>>> text = "Free Quote!\n \n Protecting your family is the best investment you\'ll eve=\nr \n"
>>> ''.join([x for x in text if x in string.ascii_letters + '\'- '])
"Free Quote  Protecting your family is the best investment you'll ever "

As you edit longer and more complex texts, excluding specific punctuation marks becomes less sustainable, and you'd need to use more complex regex (for example, when is a `'` an apostrophe or a quote?), but for the scope of your problem above, this should suffice.

Problem

I have a file which has special characters, so I used file operations to read. ``` f=open('st.txt','r') string=f.read() ``` The sample string is ``` "Free Quote!\n \n Protecting your family is the best investment you\'ll eve=\nr \n" ``` now I want to remove all the special characters and get only the words from the string. so that my string will be: ``` "Free Quote Protecting your family is the best investment you'll ever" ```

Original source