Search code examples
pythonjupyter-notebookbioinformaticsbiopythonstringio

Jupyter Notebook io.StringIO as output


Input

Blast_aa_mc = qblast("blastp","nr", aa_mc[2])
Blast_aa_mc

Output

<_io.StringIO at 0x12a1a48>

What is _io.StringIO? and what does it mean? What I was expecting was some sort of string or array. Is there a better way to do this?


Solution

  • StringIO is a class from Python's io module in the standard library. Essentially a StringIO object behaves like a Python file object that is not stored on disk but kept in memory.

    Let's see a simple example:

    f = io.StringIO("Some initial\ntext data.")
    

    If you print it out, you get a result similar to yours:

    print(f)
    
    >> <_io.StringIO object at 0x7f4530264a68>
    

    How to deal with this? Well, virtually anything that you can do with a file object, you can do with a StringIO object. For example to get the list of all lines in f:

        content = f.readlines()
        print(content)
    
        >> ['Some initial\n', 'text data.']
    

    And to get a single string containing all content:

    print(''.join(content))
    
    >> 'Some initial
    text data.'
    

    Please note that you can only call readlines once - just as it is the case for files. The second call to readlines will return an empty list.