【发布时间】:2015-05-08 05:10:13
【问题描述】:
如何将文本文件中的多个空行减少为每次出现的一行?
我已将整个文件读入一个字符串,因为我想跨行结尾进行一些替换。
with open(sourceFileName, 'rt') as sourceFile:
sourceFileContents = sourceFile.read()
这个好像不行
while '\n\n\n' in sourceFileContents:
sourceFileContents = sourceFileContents.replace('\n\n\n', '\n\n')
这也不是
sourceFileContents = re.sub('\n\n\n+', '\n\n', sourceFileContents)
将它们全部剥离很容易,但我想在每次遇到它们时将多个空行减少到一个。
我觉得我已经很接近了,但就是无法让它发挥作用。
【问题讨论】:
标签: python regex python-3.x