PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 2, 2022

[FIXED] How can I read a bytes string (i.e., gzipped text file read) to a file handle object in Python?

 November 02, 2022     byte, file, gzip, python, string     No comments   

Issue

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

Solution

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


Answered By - O.rka
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing