删除不必要字符(如空格) import re f=open(r"1.txt",'r',encoding='utf-8') alllines=f.readlines() f.close() f=open(r"output.txt",'w+',encoding='utf-8') for eachline in alllines: a=re.sub(' ','. ',eachline) f.writelines(a) f.close()