5

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?

3
  • line can have spaces or tabs and loooks like empty. Commented Nov 17, 2016 at 6:19
  • use for line in file: if not line.strip(): print("it is empty line") Commented Nov 17, 2016 at 6:21
  • Pretty sure an empty list has a False boolean value. Commented Nov 17, 2016 at 6:23

4 Answers 4

9

You can check if they are empty:

file = open('filename')
lines = [line for line in file.readlines() if line.strip()]
file.close()
Sign up to request clarification or add additional context in comments.

Comments

6
for line in file:
  if not line.strip():
    ... do something

Follwoing will be best for readinf files

with open("fname.txt") as file:
    for line in file:
      if not line.strip():
        ... do something

With open will takecare of file close.

If you want to ignore lines with only whitespace

5 Comments

When you say ...do something, can i just put the file.close there?
@BraydenHark the with clause eliminates the need for explicit file.close
So what would i put in for the do something..? or could i just leave it blank there? Sorry im extremely new to coding, and i'm not sure its for me =/
using this method I'm getting an error " List index out of range" heres the code with open("C:\\Users\\Brayd\OneDrive\\Documents\\2015HomicideLog_FINAL.txt") as file: for line in file: lines=file.readlines()[1:] if not line.strip(): lines=file.readlines()[1:] Ive tried replacing the bottom command with file.close() and print("it is empty line") and nothing seems to be working
Runs perfectly fine if i delete the spaces @ the bottom of the .txt file, but the file my Prof wants us to read has whitespaces at the bottom, any idea? @backtrack
4

Here's a very simple way to skip the empty lines:

with open(file) as f_in: 
    lines = list(line for line in (l.strip() for l in f_in) if line)

Comments

2
  1. One way is to use the lines list and remove all the elements e such that e.strip() is empty. This way, you can delete all lines with just whitespaces.
  2. Other way is to use f.readline instead of f.readlines() which will read the file line by line. First, initialize an empty list. If the present read-in line, after stripping, is empty, ignore that line and continue to read the next line. Else add the read-in line to the list.

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.