0

I am new in python and I am trying to print a list line by line.

fp = open(filepath) # Open file on read mode
lines = fp.read().split("\n")   #Create a list with each line

print(lines) #Print the list

for line in lines:
    print(line) #Print each line

fp.close()

But it's printing in one line.

The contents of the text file are

peat1,1,11345674565,04-11-2018
peat2,0,11345674565,05-11-2018
peat3,1,11345674565,06-11-2018
peat4,0,11345674565,07-11-2018

And it is printing as

peat1,1,11345674565,04-11-2018 peat2,0,11345674565,05-11-2018 peat3,1,11345674565,06-11-2018 peat4,0,11345674565,07-11-2018

The environment is -- Python 3.4 and running under Apache through cgi-bin

Any help is highly appreciated.

Prithviraj Mitra
  • 7,940
  • 9
  • 44
  • 80

1 Answers1

0

With large files, you are better off reading files line by line, like so:

with open('filename') as file: 
    for line in file:
        print(line)

I suggest this approach with with, which handles closing the file pointer itself.

Austin
  • 24,608
  • 4
  • 20
  • 43