How can I pickle a python object into a csv file?

百般思念 提交于 2019-12-08 08:17:08

问题


I am trying to pickle a python object into a csv file. I want to write the pickle of an object as the third column in my file. I want to use pickle to avoid writing serialization for my complex objects.

Code to write to csv :

 with open(self.file_path, 'a') as csv_file:
        wr = csv.writer(csv_file, delimiter='|')
        row = ['klines', symbol]
        row.extend(pickle.dumps(object))
        wr.writerow(row)

Code to read csv :

 with open(self.simulation_file_name, 'r') as csv_file:
        line = csv_file.readline()
        while line != '':
            line = line.strip('\n')
            columns = line.split('|')
            event_type = line.pop(0)
            symbol = line.pop(0)
            pickled = line.pop(0)
            klines = pickle.loads(klines)

I get the following error :

TypeError: a bytes-like object is required, not 'str'

回答1:


To write bytes/binary in text file like CSV, use base64 or other methods to avoid any escaping problem. Code simplified & python3 assumed.

import base64
with open('a.csv', 'a', encoding='utf8') as csv_file:
    wr = csv.writer(csv_file, delimiter='|')
    pickle_bytes = pickle.dumps(obj)            # unsafe to write
    b64_bytes = base64.b64encode(pickle_bytes)  # safe to write but still bytes
    b64_str = b64_bytes.decode('utf8')          # safe and in utf8
    wr.writerow(['col1', 'col2', b64_str])


# the file contains
# col1|col2|gANdcQAu

with open('a.csv', 'r') as csv_file:
    for line in csv_file:
        line = line.strip('\n')
        b64_str = line.split('|')[2]                    # take the pickled obj
        obj = pickle.loads(base64.b64decode(b64_str))   # retrieve

P.S. If you are not writing a utf8 file (e.g. ascii file), simply replace the encoding method.

P.S. Writing bytes in CSV is possible yet hardly elegant. One alternative is dumping a whole dict with dumped objects as values and storing keys in the CSV.



来源:https://stackoverflow.com/questions/48456900/how-can-i-pickle-a-python-object-into-a-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!