to_csv()和read_csv()用于包含序列化对象的数据帧 [英] to_csv() and read_csv() for dataframe containing serialized objects

查看:55
本文介绍了to_csv()和read_csv()用于包含序列化对象的数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经证明,从csv中存储并再次加载后,从熊猫数据帧的单元格存储和检索序列化对象失败:

I have proved that the storing and retrieving of a serialized object from the cell of a pandas dataframe is failing after it is stored and loaded again from csv:

a = df['cookie'].iloc[0]
print (type(a))
>> <class 'requests.cookies.RequestsCookieJar'>

然后

df.to_csv('file2.csv')
df2 = pd.read_csv('file2.csv')
b = df2['cookie'].iloc[0]
print(type(b))
>> <class 'str'>

在其单元格中,看起来只有方括号,但是

in its cell, it only looks like it differs by a square bracket but

c = '[' + b + ']'

..也无法解决.

顺便说一句:

print(pd.__version__)
>> '0.19.2'

,如果您需要这些对象之一进行测试,则可以使它像这样:

and if you need one of those objects for testing you can make one like this:

import requests
url = 'http://www.facebook.com/'
r = requests.get(url)
c = r.cookies

来自 pandas.DataFrame.to_csv 尝试添加 mode ='wb',但这只会生成一条错误消息.

From pandas.DataFrame.to_csv have tried adding mode='wb' but that only generated an error message.

pandas.read_csv 甚至不包含一种 mode 选项,因此如果该选项有效,则不确定如何将其恢复.

pandas.read_csv does not even contain a mode option so if it did work not sure how one would get it back.

有什么想法吗?

推荐答案

引用字符串是否可以解决问题?

Does quoting the string fix the issue?

import csv
df.to_csv(‘file2.csv’, csv.QUOTE_NONNUMERIC)

我不确定是否可以从中获得所需的信息,但也许...您可以将cookie转换为字典并从中获取字符串值.

I'm not sure if you can get what you need from this but maybe... You could convert the cookie to a dictionary and get the string values from there.

url = 'http://www.facebook.com/'
r = requests.get(url)
c = r.cookies
c_dict = dict(c)

这篇关于to_csv()和read_csv()用于包含序列化对象的数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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