Skip to content Skip to sidebar Skip to footer

To_csv() And Read_csv() For Dataframe Containing Serialized Objects

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'].i

Solution 1:

I don't think you can store cookies or other non trivial objects as text in normal text files / csv. However, pickle will work for you.

import pickle

# dump dataframe to a serialized pickle, df.pkl will be its filenamewithopen('df.pkl', 'wb') as output:
    pickle.dump(df, output)

# then you can load it back withwithopen('df.pkl', 'rb') as infile:
    df_from_pickle = pickle.load(infile)

Solution 2:

Does quoting the string fix the issue?

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

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)

Post a Comment for "To_csv() And Read_csv() For Dataframe Containing Serialized Objects"