Last active
May 13, 2024 07:54
-
-
Save 0ut0fcontrol/baac52eb7119d6455883451b711d3478 to your computer and use it in GitHub Desktop.
python read gzip(.gz) file line by line, compatible with python2 python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gzip | |
gzfile = "example.gz" | |
# using `with` | |
with gzip.open(gzfile) as f: | |
for i in f: | |
i = i.decode('utf-8') # for compatible with python3 | |
#do sometime | |
print(i) | |
# not `with` | |
f = gzip.open(gzfile) | |
lines = f.readlines() | |
f.close() | |
lines = [ i.decode('utf-8') for i in lines ] # for compatible with python3 | |
print(lines[-1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment