strip punctuation with regex - python

前端 未结 3 2064
太阳男子
太阳男子 2020-12-09 03:41

I need to use regex to strip punctuation at the start and end of a word. It seems like regex would be the best option for this. I don\'t want punctuation r

3条回答
  •  独厮守ぢ
    2020-12-09 04:39

    You can remove punctuation from a text file or a particular string file using regular expression as follows -

    new_data=[]
    with open('/home/rahul/align.txt','r') as f:
        f1 = f.read()
        f2 = f1.split()
    
    
    
        all_words = f2 
        punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~''' 
        # You can add and remove punctuations as per your choice 
        #removing stop words in hungarian text and  english text and 
        #display the unpunctuated string
        # To remove from a string, replace new_data with new_str 
        # new_str = "My name$#@ is . rahul -~"
    
        for word in all_words: 
            if word not in punctuations:
               new_data.append(word)
    
        print (new_data)
    

    P.S. - Do the identation properly as per required. Hope this helps!!

提交回复
热议问题