StringIO and pandas read_csv

后端 未结 1 514
广开言路
广开言路 2020-12-16 10:05

I\'m trying to mix StringIO and BytesIO with pandas and struggling with some basic stuff. For example, I can\'t get \"output\" below to work, whereas \"output2\" below does

相关标签:
1条回答
  • 2020-12-16 10:32

    io.StringIO here is behaving just like a file -- you wrote to it, and now the file pointer is pointing at the end. When you try to read from it after that, there's nothing after the point you wrote, so: no columns to parse.

    Instead, just like you would with an ordinary file, seek to the start, and then read:

    >>> output = io.StringIO()
    >>> output.write('x,y\n')
    4
    >>> output.write('1,2\n')
    4
    >>> output.seek(0)
    0
    >>> pd.read_csv(output)
       x  y
    0  1  2
    
    0 讨论(0)
提交回复
热议问题