0

My large input file is a filename.txt which has many blank lines throughout it. When I find a blank line, I want to ignore it, but when I find an EOF, I want to halt the program. I have read that there is no such thing in Python, but there must be a clever workaround.

len = 0 occurs at blank lines so can't be used to identify end of file neither can '' and for the same reason I need my loop to stop when there are no more rows to read.

I have read the other "answers", but I can't "assume" len(0) means eof or the loop won't stop

Actually playing around with variations: len blank line reports 1 / same answer with 1 char line len stripped line - 0 all len lines is true number of chars +1 (counting CRLF?) all len of stripped line is true number of chars

I want to strip my incoming at some point in the program anyway, so now I do it right after the read and the logic works. If len of stripped line = 0, I know that is a blank line and can process accordingly.

My question is answered, I'm good to go.

  • 1
    You won't get a length of zero for blank lines - they at least contain a newline character, so have a minimum length of one. (If you are applying `.strip()` or similar to the lines, then you just need to do that *after* checking the length.) – jasonharper Nov 17 '22 at 19:15
  • Do you have enough memory to use `readlines()`? – PM 77-1 Nov 17 '22 at 19:16
  • Too big for readlines() – DR ROBERT E PETERSEN Nov 17 '22 at 21:27
  • Sorry to disagree, Jason, but when readline() finds a blank line in my txt file, it prints out a 0 len, I can't use a test for len() = 0 to know when to stop. my loop just continues to run. I need a technique to keep looping over blank lines len() = 0 and end of file. – DR ROBERT E PETERSEN Nov 17 '22 at 21:33

0 Answers0