|
发表于 2021-9-18 10:20:15
|
显示全部楼层
本帖最后由 suchocolate 于 2021-9-18 10:21 编辑
通过尝试发现,用rb方式读取的文本经过decode后再写入文件,就会出现这个问题,如果是r方式读取再写入就不会。
你可以用以下两种代码尝试一下:
1. rb decode方式:- import re
- with open('32424.txt', 'r') as f:
- print('32424.txt 原始内容 f.readlines():', f.readlines())
- print('=' * 100)
- with open('32424.txt', 'rb') as f:
- txt = f.read().decode('gbk')
- print('以rb方式读取decode之后内容', repr(txt))
- print('=' * 100)
- w = re.sub(r'\{.*?\}', '', txt)
- print('替换后:', repr(w))
- print('=' * 100)
- with open('32424_result1.txt', 'w') as f:
- f.write(w)
- with open('32424_result1.txt') as f:
- print('32424_result1.txt f.readlines():', f.readlines())
- print('=' * 100)
复制代码
2. r 方式:
- import re
- with open('32424.txt', 'r') as f:
- print('32424.txt 原始内容 f.readlines():', f.readlines())
- print('=' * 100)
- with open('32424.txt', 'r') as f:
- txt = f.read()
- w = re.sub(r'\{.*?\}', '', txt)
- print('替换后:', repr(txt))
- print('=' * 100)
- with open('32424_result2.txt', 'w') as f:
- f.write(w)
- with open('32424_result2.txt') as f:
- print('32424_result2.txt f.readlines():', f.readlines())
- print('=' * 100)
复制代码
如果想规避这个问题,可以在rb方式保存文件时,使用newline=''参数:- with open('32424_result1.txt', 'w', newlin='') as f:
- f.write(w)
复制代码
|
|