Home > OS >  How can I read a bytes string (i.e., gzipped text file read) to a file handle object in Python?
How can I read a bytes string (i.e., gzipped text file read) to a file handle object in Python?

Time:10-15

I cant figure out how to treat this string representation of gzipped text as a file handle object.

import requests
def read_url(url, params=None, **kwargs):
    r = requests.get(url, params=params, **kwargs)
    return r.text

# Read gzipped file from URL
gzipped_string = read_url("https://github.com/jolespin/walkthroughs/raw/main/data/microbiome__acute-malnutrition/sequences.fasta.gz")

# Create a file handle for it
for line in StringIO(gzipped_string):
    print(line)
    # �4�_sequences.fasta�]�r�̍��i�-iFsq��G�}��@�?ͦ4�v8l���& @fE�����^������0-���K"%U�ϯ>ʟ���)��xy���E?����l���]xy���������ϟ��>ߍ�J�ee���˻,�����~������*�'�
    break

CodePudding user response:

def read_url(url, params=None, **kwargs):
    r = requests.get(url, params=params, **kwargs)
    return r.content # Note the change based on @president-james-k-polk

gzipped_string = read_url("https://github.com/jolespin/walkthroughs/raw/main/data/microbiome__acute-malnutrition/sequences.fasta.gz")

for line in StringIO(gzip.decompress(gzipped_string).decode("utf-8")):
    print(line)
    # >Otu000009
    break
  • Related