永发信息网

python 读取文本文件 删除里边的空行

答案:4  悬赏:10  手机版
解决时间 2021-02-27 01:25
  • 提问者网友:咪咪
  • 2021-02-26 10:02
123 344 456
234 566 77898
123 546 788
变为
123 344 456
234 566 77898
123 546 788

python 2.7
如果去掉里边的空行
谢谢
最佳答案
  • 五星知识达人网友:持酒劝斜阳
  • 2021-02-26 11:11
正则表达式替换所有空行(包括空格行)为空
import re
re.sub(r"\n[\s| ]*\n", '', open('d:/test.txt').read())
全部回答
  • 1楼网友:躲不过心动
  • 2021-02-26 12:52
f = open('test.txt', 'r') lines = [ _l for _l in f if _l.strip() ] f.close() f = open('lua.lua', 'w') f.writelines(lines) f.close()
  • 2楼网友:拜訪者
  • 2021-02-26 12:38
python读取一个文本文件,删除文本文件的空行代码如下: def delblankline(infile, outfile): """ delete blanklines of infile """ infp = open(infile, "r")o  utfp = open(outfile, "w") lines = infp.readlines() for li in lines: if li.split(): outfp.writelines(li) infp.close() outfp.close() #调用示例 if  __name__ == "__main__": delblankline("1.txt","2.txt")
  • 3楼网友:大漠
  • 2021-02-26 11:25
def delblankline(infile, outfile): """ Delete blanklines of infile """ infp = open(infile, "r") outfp = open(outfile, "w") lines = infp.readlines() for li in lines: if li.split(): outfp.writelines(li) infp.close() outfp.close() #调用示例 if __name__ == "__main__": delblankline("1.txt","2.txt")
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯