Tuesday, September 25, 2018

Reading line by line from a file in python




In order to extract some values from this file :



enter image description here




I need to read it line by line.
I tried to read line by line first but i don't know why it doesn't work.



I tried this :



#! /usr/bin/python
file = open('/home/results/err.txt')
for line in file.readline():
print line



EDIT:



Problem: working but not showing this lines (this is the file)
enter image description here



Just the last line of them which is: (this is what is generated)
enter image description here


Answer



You need to iterate through the file rather than the line:




#! /usr/bin/python
file = open('/home/results/err.txt')
for line in file:
print line


file.readline() only reads the first line. When you iterate over it, you are iterating over the characters in the line.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...