擅长:python、mysql、java
<p>我不明白为什么这么难。</p>
<p>你到底在干什么?请解释“最终它读到一个无效字符”。</p>
<p>应该简单到:</p>
<pre><code>import gzip
fp = gzip.open('foo.gz')
contents = fp.read() # contents now has the uncompressed bytes of foo.gz
fp.close()
u_str = contents.decode('utf-8') # u_str is now a unicode string
</code></pre>
<hr/>
<h2>编辑</h2>
<p>这个答案对<code>Python3</code>中的<code>Python2</code>有效,请参见<a href="https://stackoverflow.com/a/19794943/610569">https://stackoverflow.com/a/19794943/610569</a>上的@SeppoEnarvi的答案(它使用<code>rt</code>模式进行<code>gzip.open</code>)。</p>