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

后端 未结 5 1520
误落风尘
误落风尘 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:12

    I think nfn neil answer is great...but i would just add a simple regex to remove all no words character,however it will consider underscore as part of the word

    print  re.sub(r'\W+', ' ', string)
    >>> hello there A Z R_T world welcome to python
    

提交回复
热议问题