Q:

How to get rid of everything except letters in python?

I have a text file like this:
Sally ... went .. to the store !!# and bought an apple and a bagel..@@
Then she went home.

How do I remove all the extra things like "..." and ".." and "@" and get the words as items in a list?
I tried
filename.rstrip().split()

A:

Use a regular expression. In python, this is done with the re module.
[^a-zA-Z ] means characters that aren't letters (or space). You can use re.sub to replace all of them with whitespace:
import re

def remove_non_letters(string):
    return re.sub("[^a-zA-Z] ", " ", string).split()

EDIT:
Read in the text file with
with open('data.txt', 'r') as file:
    string = file.read()
    result = remove_non_letters(string)

result is the list you are looking for.

