jeudi 31 octobre 2019

stripping punctuation and finding unique words in Python

So my task is as such:

Write a program that displays a list of all the unique words found in the file uniq_words.txt. Print your results in alphabetic order and lowercase. Hint: Store words as the elements of a set; remove punctuations by using the string.punctuation from the string module.

Currently, the code that I have is:

def main():
    import string

    with open('uniq_words.txt') as content:
        new = sorted(set(content.read().split()))
        for i in new:
            while i in string.punctuation:
                new.discard(i)
                print(new)

main()

If I run the code as such, it goes into an infinite loop printing the unique words over and over again. There sre still words in my set that appear as i.e "value." or "never/have". How do I remove the punctuation with the string.punctuation module? Or am I approaching this from a wrong direction? Would appreciate any advice!

Edit: The link does not help me, in that the method given does not work in a list.

Aucun commentaire:

Enregistrer un commentaire