Setting up 'encoding' in Python's gzip.open() doesn't seem to work
<p>Even tho I tried to specify encoding in python's gzip.open(), it seems to be always using cp1252.py to encode the file's content.
My code:</p>

<pre><code>with gzip.open('file.gz', 'rt', 'cp1250') as f:
    content = f.read()
</code></pre>

<p>Response:</p>

<blockquote>
  <p>File "C:\Python34\lib\encodings\cp1252.py", line 23, in decode
  return codecs.charmap_decode(input,self.errors,decoding_table)[0]
  UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position    52893: character maps to undefined</p>
</blockquote>

<h1>Python 3.x</h1>

<p><code>gzip.open</code> is <a href="https://docs.python.org/3.4/library/gzip.html#gzip.open" rel="nofollow">defined</a> as:</p>

<blockquote>
  <p>gzip.open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)</p>
</blockquote>

<p>Therefore, <code>gzip.open('file.gz', 'rt', 'cp1250')</code> sends it these arguments:
- filename = 'file.gz'
- mode = 'rt'
- compresslevel = 'cp1250'</p>

<p>This is clearly wrong, because the intention is to use 'cp1250' encoding.
The <code>encoding</code> argument can either be sent as the fourth positional argument or as a keyword argument:</p>

<pre><code>gzip.open('file.gz', 'rt', 5, 'cp1250')  # 4th positional argument

gzip.open('file.gz', 'rt', encoding='cp1250') # keyword argument
</code></pre>

<h1>Python 2.x</h1>

<p><a href="https://docs.python.org/2/library/gzip.html#gzip.open" rel="nofollow">Python 2 version of <code>gzip.open</code></a> does not take the <code>encoding</code> argument and it does not accept text modes, so the decoding has to be done explicitly after reading the data:</p>

<pre><code>with gzip.open('file.gz', 'rb') as f:
    data = f.read()

decoded_data = data.decode('cp1250')
</code></pre>

python python-3.x character-encoding gzip