pythonweb-scrapingbeautifulsoupdata-cleaningpreprocessor

how to remove punctuation from list


can I any one help me with this code now I get 'test2' as same as 'test', if I test is string it works good but as list it not working properly

 punc = set(string.punctuation)
 test=[" course content good though textbook badly written.not got energy 
 though seems good union.it distance course i'm sure facilities.n/an/ain 
last year offer poor terms academic personal. seems un become overwhelmed trying become run"]

test2 = ''.join(w for w in test if w not in punc)
 print(test2)

I want to remove all punctuation


Solution

  • import string
    test=[" course content good though textbook badly written.not got energy though seems good union.it distance course i'm sure facilities.n/an/ain last year offer poor terms academic personal. seems un become overwhelmed trying become run"]
    test2 = ''.join(w for w in test[0] if w not in string.punctuation )
    print(test2)
    

    If there are multiple strings inside the list

    import string
    test=["Hi There!"," course content good though textbook badly written.not got energy though seems good union.it distance course i'm sure facilities.n/an/ain last year offer poor terms academic personal. seems un become overwhelmed trying become run"]
    #if there are multiple string in the list
    for x in test:
        print(''.join(w for w in x if w not in string.punctuation ))
    # If there are multiple strings in the list and you want to join all of them togather
    print(''.join(w for w in [x for x in test] if w not in string.punctuation )) 
    

    If you need to append it to a list variable

    import string
    test2=[]
    test=["Hi There!"," course content good though textbook badly written.not got energy though seems good union.it distance course i'm sure facilities.n/an/ain last year offer poor terms academic personal. seems un become overwhelmed trying become run"]
    #if there are multiple string in the list
    for x in test:
        test2.append(''.join(w for w in x if w not in string.punctuation ))
    print(test2)