clean line of punctuation and split into words python
Asked Answered
F

2

6

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.

Fizgig answered 26/10, 2012 at 16:27 Comment(0)
B
4

You should set cleanWord back to an empty string at the top of each iteration of the outer loop:

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

Note that I also removed the if word != " ", since after line.split() you will never have spaces.

>>> createWordList('please. let! this3 work.')
['please', 'let', 'this', 'work']
Bobinette answered 26/10, 2012 at 16:32 Comment(1)
Thanks the cleanWord reset I saw about a minute ago and realized my mistake there thank you greatly guysFizgig
M
12

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']
Magalymagan answered 26/10, 2012 at 16:36 Comment(1)
Thanks @Lanaru, of course a character range makes more sense here (I had just copied and pasted his "unwanted characters").Magalymagan
B
4

You should set cleanWord back to an empty string at the top of each iteration of the outer loop:

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

Note that I also removed the if word != " ", since after line.split() you will never have spaces.

>>> createWordList('please. let! this3 work.')
['please', 'let', 'this', 'work']
Bobinette answered 26/10, 2012 at 16:32 Comment(1)
Thanks the cleanWord reset I saw about a minute ago and realized my mistake there thank you greatly guysFizgig

© 2022 - 2024 — McMap. All rights reserved.