tejesh s
tejesh s

Reputation: 121

unable to read large bz2 file

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

Answers (2)

martineau
martineau

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

James Bear
James Bear

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

Related Questions