I checked this for a short while, and it seems that everything will go right. You can provide the obj file for gzip.GzipFile and io.open , therefore
import io import gzip f_obj = open('file.gz','r') io_obj = io.open(f_obj.fileno(), encoding='UTF-8') gzip_obj = gzip.GzipFile(fileobj=io_obj, mode='r') gzip_obj.read()
This gives me a UnicodeDecodeError , because the file I am reading is not really UTF-8, so it seems to be doing the right thing.
For some reason, if I use io.open to open file.gz directly, gzip says the file is not a compressed file.
UPDATE Yes, this is stupid, from the very beginning threads are the wrong way.
test file
ΓΆ Γ€ u y
The following code decodes a compressed file with a specific codec
import codecs import gzip gz_fh = gzip.open('file.gz') ascii = codecs.getreader('ASCII') utf8 = codecs.getreader('UTF-8') ascii_fh = ascii(gz_fh) utf8_fh = utf8(gz_fh) ascii_fh.readlines() -> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128) utf8_fh.readlines() -> [u'\xf6\n', u'\xe4\n', u'u\n', u'y']
codecs.StreamReader takes a stream, so you must transfer compressed or uncompressed files to it.
http://docs.python.org/library/codecs.html#codecs
source share