StringIO和 pandas read_csv [英] StringIO and pandas read_csv

查看:62
本文介绍了StringIO和 pandas read_csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将StringIO和BytesIO与熊猫混合使用,并尝试使用一些基本的东西.例如,我不能在下面使用输出"来工作,而在下面可以使用"output2"来工作.但是输出"更接近于我要尝试的真实示例. "output2"中的方式来自一个古老的熊猫示例,但对我而言并不是真正有用的方法.

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 work. But "output" is closer to the real world example I'm trying to do. The way in "output2" is from an old pandas example but not really a useful way for me to do it.

import io   # note for python 3 only
            # in python2 need to import StringIO

output = io.StringIO()
output.write('x,y\n')
output.write('1,2\n')

output2 = io.StringIO("""x,y
1,2
""")

它们在类型和内容上似乎是相同的:

They seem to be the same in terms of type and contents:

type(output) == type(output2)
Out[159]: True

output.getvalue() == output2.getvalue()
Out[160]: True

但不,不一样:

output == output2
Out[161]: False

关于我要解决的问题的更多信息:

More to the point of the problem I'm trying to solve:

pd.read_csv(output)   # ValueError: No columns to parse from file
pd.read_csv(output2)  # works fine, same as reading from a file

推荐答案

io.StringIO此处的行为就像文件一样-您已写入文件,现在文件指针指向末尾.之后,当您尝试从中读取内容时,编写的内容之后没有任何内容,因此:没有要解析的列.

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.

相反,就像处理普通文件一样,以seek开头,然后阅读:

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

这篇关于StringIO和 pandas read_csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆