Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read gz file as a string in Python

I have been searching for a way that could read gz file in python, and I did something like

with gzip.open(filepath) as infile:
    ...

However, it seems like the read-in data is byte-like and I cannot do something like for l in infile. Is there a way to solve this?

like image 757
John M. Avatar asked Oct 30 '25 22:10

John M.


1 Answers

Pass mode='rt' (text mode) to the gzip.open call.

From the documentation (linked above):

The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x' or 'xb' for binary mode, or 'rt', 'at', 'wt', or 'xt' for text mode. The default is 'rb'.

like image 184
Joe Iddon Avatar answered Nov 01 '25 12:11

Joe Iddon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!