跳过读取文件python中的空行

编程入门 行业动态 更新时间:2024-10-26 01:28:22
本文介绍了跳过读取文件python中的空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在做一个很长的项目,我已经完成了所有工作,但是在他希望我们在底部阅读的文件中,有空格,合法只是我们不允许删除的空格,以便工作在项目中我删除了它们,因为我不知道如何解决它,所以我当前的打开/读取看起来像这样

Im working on a very long project, i have everything done with it, but in the file he wants us to read at the bottom there are empty spaces, legit just blank spaces that we aren't allowed to delete, to work on the project i deleted them because i have no idea how to get around it, so my current open/read looks like this

file = open("C:\\Users\\bh1337\\Documents\\2015HomicideLog_FINAL.txt" , "r") lines=file.readlines()[1:] file.close()

我需要添加什么来忽略空行?还是在遇到空行时停止?

What do i need to add to this to ignore blank lines? or to stop when it gets to a blank line?

推荐答案

您可以检查它们是否为空:

You can check if they are empty:

file = open('filename') lines = [line for line in file.readlines() if line.strip()] file.close()

更多推荐

跳过读取文件python中的空行

本文发布于:2023-05-25 10:57:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/226650.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空行   跳过   文件   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!