print list line by line in python [duplicate]












0















This question already has an answer here:




  • How to a read large file, line by line in Python

    10 answers




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.










share|improve this question















marked as duplicate by Red Cricket, Pedro Lobito, Nic3500, Taylor Edmiston, khachik Nov 23 '18 at 20:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 4




    It should work. Perhaps your file only has 1 line? How many elements does your lines list contain? Can you include a portion of the file as well?
    – slider
    Nov 23 '18 at 18:09










  • Use print(len(lines)) to check the how many lines file has.
    – Mauro Baraldi
    Nov 23 '18 at 18:14










  • @RedCricket Before posting here I have explored 3-4 similar posts but not the one which you mentioned. The reason is my txt file has got only 4 lines so it's not a large file.
    – Prithviraj Mitra
    Nov 23 '18 at 18:15






  • 1




    @PrithvirajMitra your approach should work so there is something you aren't showing us. You really need to provide a Minimal, Complete, and Verifiable example, and at the very least, the output you are seeing.
    – juanpa.arrivillaga
    Nov 23 '18 at 18:16






  • 2




    @PrithvirajMitra It's working fine for me: trinket.io/python/23a9bb3d00
    – slider
    Nov 23 '18 at 18:33
















0















This question already has an answer here:




  • How to a read large file, line by line in Python

    10 answers




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.










share|improve this question















marked as duplicate by Red Cricket, Pedro Lobito, Nic3500, Taylor Edmiston, khachik Nov 23 '18 at 20:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 4




    It should work. Perhaps your file only has 1 line? How many elements does your lines list contain? Can you include a portion of the file as well?
    – slider
    Nov 23 '18 at 18:09










  • Use print(len(lines)) to check the how many lines file has.
    – Mauro Baraldi
    Nov 23 '18 at 18:14










  • @RedCricket Before posting here I have explored 3-4 similar posts but not the one which you mentioned. The reason is my txt file has got only 4 lines so it's not a large file.
    – Prithviraj Mitra
    Nov 23 '18 at 18:15






  • 1




    @PrithvirajMitra your approach should work so there is something you aren't showing us. You really need to provide a Minimal, Complete, and Verifiable example, and at the very least, the output you are seeing.
    – juanpa.arrivillaga
    Nov 23 '18 at 18:16






  • 2




    @PrithvirajMitra It's working fine for me: trinket.io/python/23a9bb3d00
    – slider
    Nov 23 '18 at 18:33














0












0








0








This question already has an answer here:




  • How to a read large file, line by line in Python

    10 answers




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.










share|improve this question
















This question already has an answer here:




  • How to a read large file, line by line in Python

    10 answers




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.





This question already has an answer here:




  • How to a read large file, line by line in Python

    10 answers








python list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 18:18

























asked Nov 23 '18 at 18:06









Prithviraj Mitra

2,48472248




2,48472248




marked as duplicate by Red Cricket, Pedro Lobito, Nic3500, Taylor Edmiston, khachik Nov 23 '18 at 20:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Red Cricket, Pedro Lobito, Nic3500, Taylor Edmiston, khachik Nov 23 '18 at 20:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 4




    It should work. Perhaps your file only has 1 line? How many elements does your lines list contain? Can you include a portion of the file as well?
    – slider
    Nov 23 '18 at 18:09










  • Use print(len(lines)) to check the how many lines file has.
    – Mauro Baraldi
    Nov 23 '18 at 18:14










  • @RedCricket Before posting here I have explored 3-4 similar posts but not the one which you mentioned. The reason is my txt file has got only 4 lines so it's not a large file.
    – Prithviraj Mitra
    Nov 23 '18 at 18:15






  • 1




    @PrithvirajMitra your approach should work so there is something you aren't showing us. You really need to provide a Minimal, Complete, and Verifiable example, and at the very least, the output you are seeing.
    – juanpa.arrivillaga
    Nov 23 '18 at 18:16






  • 2




    @PrithvirajMitra It's working fine for me: trinket.io/python/23a9bb3d00
    – slider
    Nov 23 '18 at 18:33














  • 4




    It should work. Perhaps your file only has 1 line? How many elements does your lines list contain? Can you include a portion of the file as well?
    – slider
    Nov 23 '18 at 18:09










  • Use print(len(lines)) to check the how many lines file has.
    – Mauro Baraldi
    Nov 23 '18 at 18:14










  • @RedCricket Before posting here I have explored 3-4 similar posts but not the one which you mentioned. The reason is my txt file has got only 4 lines so it's not a large file.
    – Prithviraj Mitra
    Nov 23 '18 at 18:15






  • 1




    @PrithvirajMitra your approach should work so there is something you aren't showing us. You really need to provide a Minimal, Complete, and Verifiable example, and at the very least, the output you are seeing.
    – juanpa.arrivillaga
    Nov 23 '18 at 18:16






  • 2




    @PrithvirajMitra It's working fine for me: trinket.io/python/23a9bb3d00
    – slider
    Nov 23 '18 at 18:33








4




4




It should work. Perhaps your file only has 1 line? How many elements does your lines list contain? Can you include a portion of the file as well?
– slider
Nov 23 '18 at 18:09




It should work. Perhaps your file only has 1 line? How many elements does your lines list contain? Can you include a portion of the file as well?
– slider
Nov 23 '18 at 18:09












Use print(len(lines)) to check the how many lines file has.
– Mauro Baraldi
Nov 23 '18 at 18:14




Use print(len(lines)) to check the how many lines file has.
– Mauro Baraldi
Nov 23 '18 at 18:14












@RedCricket Before posting here I have explored 3-4 similar posts but not the one which you mentioned. The reason is my txt file has got only 4 lines so it's not a large file.
– Prithviraj Mitra
Nov 23 '18 at 18:15




@RedCricket Before posting here I have explored 3-4 similar posts but not the one which you mentioned. The reason is my txt file has got only 4 lines so it's not a large file.
– Prithviraj Mitra
Nov 23 '18 at 18:15




1




1




@PrithvirajMitra your approach should work so there is something you aren't showing us. You really need to provide a Minimal, Complete, and Verifiable example, and at the very least, the output you are seeing.
– juanpa.arrivillaga
Nov 23 '18 at 18:16




@PrithvirajMitra your approach should work so there is something you aren't showing us. You really need to provide a Minimal, Complete, and Verifiable example, and at the very least, the output you are seeing.
– juanpa.arrivillaga
Nov 23 '18 at 18:16




2




2




@PrithvirajMitra It's working fine for me: trinket.io/python/23a9bb3d00
– slider
Nov 23 '18 at 18:33




@PrithvirajMitra It's working fine for me: trinket.io/python/23a9bb3d00
– slider
Nov 23 '18 at 18:33












1 Answer
1






active

oldest

votes


















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.






share|improve this answer























  • There approach should work, though, even if it is less than ideal. And in any event, if this were the answer, then it would be a clear duplicate and should be closed
    – juanpa.arrivillaga
    Nov 23 '18 at 18:10












  • @juanpa.arrivillaga, I added a reason to use this approach than OPs. :)
    – Austin
    Nov 23 '18 at 18:11










  • But that doesn't answer the purported question, because the OP claims the approach doesn't work, and indeed if it doesn't, then this approach wouldn't work either. The OP needs to add more details and a MCVE
    – juanpa.arrivillaga
    Nov 23 '18 at 18:12






  • 1




    @MauroBaraldi not in Python 3.
    – juanpa.arrivillaga
    Nov 23 '18 at 18:14






  • 2




    @MauroBaraldi, stackoverflow.com/questions/24942358/…
    – Austin
    Nov 23 '18 at 18:15


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer























  • There approach should work, though, even if it is less than ideal. And in any event, if this were the answer, then it would be a clear duplicate and should be closed
    – juanpa.arrivillaga
    Nov 23 '18 at 18:10












  • @juanpa.arrivillaga, I added a reason to use this approach than OPs. :)
    – Austin
    Nov 23 '18 at 18:11










  • But that doesn't answer the purported question, because the OP claims the approach doesn't work, and indeed if it doesn't, then this approach wouldn't work either. The OP needs to add more details and a MCVE
    – juanpa.arrivillaga
    Nov 23 '18 at 18:12






  • 1




    @MauroBaraldi not in Python 3.
    – juanpa.arrivillaga
    Nov 23 '18 at 18:14






  • 2




    @MauroBaraldi, stackoverflow.com/questions/24942358/…
    – Austin
    Nov 23 '18 at 18:15
















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.






share|improve this answer























  • There approach should work, though, even if it is less than ideal. And in any event, if this were the answer, then it would be a clear duplicate and should be closed
    – juanpa.arrivillaga
    Nov 23 '18 at 18:10












  • @juanpa.arrivillaga, I added a reason to use this approach than OPs. :)
    – Austin
    Nov 23 '18 at 18:11










  • But that doesn't answer the purported question, because the OP claims the approach doesn't work, and indeed if it doesn't, then this approach wouldn't work either. The OP needs to add more details and a MCVE
    – juanpa.arrivillaga
    Nov 23 '18 at 18:12






  • 1




    @MauroBaraldi not in Python 3.
    – juanpa.arrivillaga
    Nov 23 '18 at 18:14






  • 2




    @MauroBaraldi, stackoverflow.com/questions/24942358/…
    – Austin
    Nov 23 '18 at 18:15














0












0








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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 18:11

























answered Nov 23 '18 at 18:09









Austin

9,5923828




9,5923828












  • There approach should work, though, even if it is less than ideal. And in any event, if this were the answer, then it would be a clear duplicate and should be closed
    – juanpa.arrivillaga
    Nov 23 '18 at 18:10












  • @juanpa.arrivillaga, I added a reason to use this approach than OPs. :)
    – Austin
    Nov 23 '18 at 18:11










  • But that doesn't answer the purported question, because the OP claims the approach doesn't work, and indeed if it doesn't, then this approach wouldn't work either. The OP needs to add more details and a MCVE
    – juanpa.arrivillaga
    Nov 23 '18 at 18:12






  • 1




    @MauroBaraldi not in Python 3.
    – juanpa.arrivillaga
    Nov 23 '18 at 18:14






  • 2




    @MauroBaraldi, stackoverflow.com/questions/24942358/…
    – Austin
    Nov 23 '18 at 18:15


















  • There approach should work, though, even if it is less than ideal. And in any event, if this were the answer, then it would be a clear duplicate and should be closed
    – juanpa.arrivillaga
    Nov 23 '18 at 18:10












  • @juanpa.arrivillaga, I added a reason to use this approach than OPs. :)
    – Austin
    Nov 23 '18 at 18:11










  • But that doesn't answer the purported question, because the OP claims the approach doesn't work, and indeed if it doesn't, then this approach wouldn't work either. The OP needs to add more details and a MCVE
    – juanpa.arrivillaga
    Nov 23 '18 at 18:12






  • 1




    @MauroBaraldi not in Python 3.
    – juanpa.arrivillaga
    Nov 23 '18 at 18:14






  • 2




    @MauroBaraldi, stackoverflow.com/questions/24942358/…
    – Austin
    Nov 23 '18 at 18:15
















There approach should work, though, even if it is less than ideal. And in any event, if this were the answer, then it would be a clear duplicate and should be closed
– juanpa.arrivillaga
Nov 23 '18 at 18:10






There approach should work, though, even if it is less than ideal. And in any event, if this were the answer, then it would be a clear duplicate and should be closed
– juanpa.arrivillaga
Nov 23 '18 at 18:10














@juanpa.arrivillaga, I added a reason to use this approach than OPs. :)
– Austin
Nov 23 '18 at 18:11




@juanpa.arrivillaga, I added a reason to use this approach than OPs. :)
– Austin
Nov 23 '18 at 18:11












But that doesn't answer the purported question, because the OP claims the approach doesn't work, and indeed if it doesn't, then this approach wouldn't work either. The OP needs to add more details and a MCVE
– juanpa.arrivillaga
Nov 23 '18 at 18:12




But that doesn't answer the purported question, because the OP claims the approach doesn't work, and indeed if it doesn't, then this approach wouldn't work either. The OP needs to add more details and a MCVE
– juanpa.arrivillaga
Nov 23 '18 at 18:12




1




1




@MauroBaraldi not in Python 3.
– juanpa.arrivillaga
Nov 23 '18 at 18:14




@MauroBaraldi not in Python 3.
– juanpa.arrivillaga
Nov 23 '18 at 18:14




2




2




@MauroBaraldi, stackoverflow.com/questions/24942358/…
– Austin
Nov 23 '18 at 18:15




@MauroBaraldi, stackoverflow.com/questions/24942358/…
– Austin
Nov 23 '18 at 18:15



Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)