<p>You can use <a href="https://docs.python.org/3/library/gzip.html#gzip.decompress" rel="nofollow noreferrer">gzip.decompress</a>:</p>

<pre><code>import tarfile, os, gzip
import sys
tar = tarfile.open("arXiv_src_9107_001a.tar")
n = 0
for member in tar.getmembers():
    #Skip directory labeled at the top
    if(n==0):
        n=1
        continue
    f=tar.extractfile(member)
    print(member)
    content=f.read()
    expanded = gzip.decompress(content)
    # do whatever with expanded here
tar.close()
</code></pre>
Extracting compressed gz file from tar archive in python
<p>I'm trying to extract a compressed file from a tar archive using Python 3.6.5.  </p>

<p>I'm trying to extract files from a tar archive that contains compressed gz files. I've followed the advice <a href="https://stackoverflow.com/a/2018576/4021436">this</a> Stackoverflow answer : </p>

<pre><code>import tarfile,os
import sys
tar = tarfile.open("arXiv_src_9107_001a.tar")
n = 0
for member in tar.getmembers():
    #Skip directory labeled at the top
    if(n==0):
        n=1
        continue
    f=tar.extractfile(member)
    print(member)
    content=f.read()
    print("{} has {} newlines".format(member, content.count("\n")))
    print("{} has {} spaces".format(member, content.count(" ")))
    print("{} has {} characters".format(member, len(content)))
    #sys.exit()
tar.close()
</code></pre>

<p>When I print out <code>vars(tar)</code> in <code>pdb</code></p>

<pre><code>(Pdb) p vars(tar)
{'mode': 'r', '_mode': 'rb', '_extfileobj': False, 'name': '/Users/user/Downloads/arXiv_src_9107_001a.tar', 'fileobj': &lt;_io.BufferedReader name='arXiv_src_9107_001a.tar'&gt;, 'errors': 'surrogateescape', 'pax_headers': {}, 'copybufsize': None, 'closed': False, 'members': [&lt;TarInfo '9107' at 0x11004b048&gt;, &lt;TarInfo '9107/hep-lat9107001.gz' at 0x11004b110&gt;, &lt;TarInfo '9107/hep-lat9107002.gz' at 0x11004b1d8&gt;, &lt;TarInfo '9107/qc_01.gz' at 0x11004b2a0&gt;, &lt;TarInfo '9107/qc_02.gz' at 0x11004b368&gt;, &lt;TarInfo '9107/qi_01.gz' at 0x11004b430&gt;, &lt;TarInfo '9107/qs_01.gz' at 0x11004b4f8&gt;, &lt;TarInfo '9107/quant_only_01.gz' at 0x11004b5c0&gt;], '_loaded': True, 'offset': 69120, 'inodes': {}, 'firstmember': None}
</code></pre>

<p>If I print out the <code>content</code> variable, I get a bytes object.  E.g. : </p>

<pre><code>b'\x1f\x8b\x08\x08\xe5C\x12M\x00\x03hep-lat9107001\x00\xed}{w\xdbF\x92\xef\xfc\x1b|\x8a\xbe\xf72\x13i#R\x00\x08\xf0\x91\x8c\xf7\x1c?c\xcf\xc6\x8f\xb5\x9d\xc9\xeeZN\x06"!\tc\x92\xe0\x10\xa0d\x85W\xf9\xec\xf...
</code></pre>

<p><strong>Question</strong></p>

<p>In the case where a tar archive is composed of individually compressed files, how do I read/decompress those gz files into usable human language strings?</p>