Saturday, 1 July 2017

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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...