Reputation: 121
I am trying to read a large bz2 file with this code:
import bz2
file= bz2.BZ2File("20150219.csv.bz2","rb")
print file.read()
file.close()
But after 4525 lines, it stops without an error message. The bz2 file is much bigger.
How can I read the whole file line by line?
Upvotes: 1
Views: 2074
Reputation: 123531
Your file.read()
call tries to read the entire file into memory and then and decompress all of it there, too. Try reading it a line at a time:
import bz2
with bz2.BZ2File("20150219.csv.bz2", "r") as file:
for line in file:
print(line)
Upvotes: 2
Reputation: 444
Why do you want to print a binary file line by line? Read them to a bytes object instead:
bs = file.read()
Upvotes: 1