print list line by line in python [duplicate]
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.
python list
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.
|
show 5 more comments
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.
python list
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 yourlines
list contain? Can you include a portion of the file as well?
– slider
Nov 23 '18 at 18:09
Useprint(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
|
show 5 more comments
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.
python list
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
python list
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 yourlines
list contain? Can you include a portion of the file as well?
– slider
Nov 23 '18 at 18:09
Useprint(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
|
show 5 more comments
4
It should work. Perhaps your file only has 1 line? How many elements does yourlines
list contain? Can you include a portion of the file as well?
– slider
Nov 23 '18 at 18:09
Useprint(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
|
show 5 more comments
1 Answer
1
active
oldest
votes
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.
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
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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
|
show 1 more comment
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.
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
|
show 1 more comment
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.
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.
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
|
show 1 more comment
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
|
show 1 more comment
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