def line(char, n):
    return ''.join(['%s' % char * n, '\n'])
filename = 'test.zip'
FOUT = gzip.open(filename, 'wb')
FOUT.write(line('a', 2))
FOUT.write(line('b', 3))
FOUT.write(line('c', 5))
FOUT.write(line('d', 5))
FOUT.write(line('e', 5))
FOUT.close()
length = 2 + 3 + 3 * 5 + 5
FIN = gzip.open(filename, 'rb')
for n in range(7):
    FIN.seek(random.randrange(length), 0)
print('Position %3i, char: %s' % (FIN.tell(), [FIN.read(1)]))
How to get a random line from within a gzip compressed file in python without reading it into memory