How to remove special characters from txt files using Python

后端 未结 3 1809
南笙
南笙 2021-01-06 04:32
from glob import glob
pattern = \"D:\\\\report\\\\shakeall\\\\*.txt\"
filelist = glob(pattern)
def countwords(fp):
    with open(fp) as fh:
        return len(fh.rea         


        
3条回答
  •  半阙折子戏
    2021-01-06 05:06

    import re
    string = open('a.txt').read()
    new_str = re.sub('[^a-zA-Z0-9\n\.]', ' ', string)
    open('b.txt', 'w').write(new_str)
    

    It will change every non alphanumeric char to white space.

提交回复
热议问题