How to remove special characters except space from a file in python?

后端 未结 5 1556
误落风尘
误落风尘 2021-02-19 03:49

I have a huge corpus of text (line by line) and I want to remove special characters but sustain the space and structure of the string.

hello? there A-Z-R_T(,**)         


        
5条回答
  •  故里飘歌
    2021-02-19 04:21

    you can try this

    import re
    sentance = '''hello? there A-Z-R_T(,**), world, welcome to python. this **should? the next line#followed- by@ an#other %million^ %%like $this.'''
    res = re.sub('[!,*)@#%(&$_?.^]', '', sentance)
    print(res)
    

    re.sub('["]') -> here you can add which symbol you want to remove

提交回复
热议问题