Skip to content

Instantly share code, notes, and snippets.

@0ut0fcontrol
Last active May 13, 2024 07:54
Show Gist options
  • Save 0ut0fcontrol/baac52eb7119d6455883451b711d3478 to your computer and use it in GitHub Desktop.
Save 0ut0fcontrol/baac52eb7119d6455883451b711d3478 to your computer and use it in GitHub Desktop.
python read gzip(.gz) file line by line, compatible with python2 python3
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