代码如下:

"""
读取存在空行的文件,删除其中的空行,并将其保存到新的文件中
"""
def rewrite():
with open('src-test.txt','r',encoding = 'utf-8') as fr,open('newsrc-test.txt','w',encoding = 'utf-8') as fd:
for text in fr.readlines():
if text.split():
fd.write(text)
print('输出成功....')
with open('src-train.txt','r',encoding = 'utf-8') as fr,open('newsrc-train.txt','w',encoding = 'utf-8') as fd:
for text in fr.readlines():
if text.split():
fd.write(text)
print('输出成功....')
with open('src-val.txt','r',encoding = 'utf-8') as fr,open('newsrc-val.txt','w',encoding = 'utf-8') as fd:
for text in fr.readlines():
if text.split():
fd.write(text)
print('输出成功....')