I essentially have the same problem as this guy : person also having issues iterating
Depending on what I change, I will either run into an IOError, a ValueError (when I use a for each to iterate through each line in the file, and read using readline()), or the program works but it cuts off my data when there's an empty line. I've also tried using the for each loop to iterate through the file with .next() instead of readline, but that skips just about every other line in my data set. I believe top comment there has the solution to my question, except my text file will have lines that are empty, which ends the while loop too early. What is the best way around this? Is there a better data structure to use, or do I have to somehow parse my file to remove empty lines?
Here's a segment of my code, I'm using .rstrip() to get rid of the newline characters at the end of each line:
f = open(self.path,'r')
while True:
line = f.readline().rstrip()
temp_lines_list.append(line)
if not line:
break
Some sample input:
text1 : 2380218302
test2 : sad
test3 : moresad (very)
yetanothertest : more datapoints
wowanewsection: incredible
I hope this helps thank you :)
The readline()
method returns a line with a trailing newline character, even on an empty line. You should check if the line is empty before you strip it instead:
while True:
line = f.readline()
if not line:
break
temp_lines_list.append(line.rstrip())
However, it is more idiomatic in Python to use the file object as an iterable to iterate through the lines of a file, so that you don't have to manage the iterations on your own.
for line in f:
temp_lines_list.append(line.rstrip())