当前位置 : 主页 > 编程语言 > java >

使用python实现\记事本中去除掉空行数据

来源:互联网 收集:自由互联 发布时间:2022-07-07
代码如下: """ 读取存在空行的文件,删除其中的空行,并将其保存到新的文件中 """ def rewrite(): with open('src-test.txt','r',encoding = 'utf-8') as fr,open('newsrc-test.txt','w',encoding = 'utf-8') as fd: for


代码如下:

"""
读取存在空行的文件,删除其中的空行,并将其保存到新的文件中
"""
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('输出成功....')
上一篇:ACFG
下一篇:没有了
网友评论