clean line of punctuation and split into words python

Procrastinator picture Procrastinator · Oct 26, 2012 · Viewed 7.6k times · Source

learning python currently and having a bit of a problem. I'm trying to take a line from another subprogram and convert it into separate words that have been stripped of their punctuation besides a few. the output of this program is supposed to be the word and the line numbers it shows up on. Should look like this -> word: [1]

input file:

please. let! this3 work.
I: hope. it works
and don't shut up

Code:

    def createWordList(line):
        wordList2 =[]
        wordList1 = line.split()
        cleanWord = ""
        for word in wordList1: 
            if word != " ":
                for char in word:
                    if char in '!,.?":;0123456789':
                        char = ""
                    cleanWord += char
                    print(cleanWord," cleaned")
                wordList2.append(cleanWord)
         return wordList2

output:

anddon't:[3]
anddon'tshut:[3]
anddon'tshutup:[3]
ihope:[2]
ihopeit:[2]
ihopeitworks:[2]
pleaselet:[1]
pleaseletthis3:[1]
pleaseletthis3work:[1]

I'm unsure what this is caused by but I learned Ada and transitioning to python in a short period of time.

Answer

Tim Pietzcker picture Tim Pietzcker · Oct 26, 2012

Of course, you could also use a regular expression:

>>> import re
>>> s = """please. let! this3 work.
... I: hope. it works
... and don't shut up"""
>>> re.findall(r'[^\s!,.?":;0-9]+', s)
['please', 'let', 'this', 'work', 'I', 'hope', 'it', 'works', 'and', "don't", 
 'shut', 'up']