I am working on a python file that requires large data but I am not able to edit my data. Can someone suggest me a code on how can I convert a .pkl file to .csv file.
Converting .pkl file to .csv file
Asked Answered
Welcome to StackOverflow. your question is lacking. im going to assume that by pkl you mean a python pickle file, which can contain just about anything, what does the data in it look like? what have you tried so far? –
Bobsled
Yes it is a python pickle file. It is an emotion recording file in 10 persons. –
Milklivered
yes but what does it look like? is its a list of something? is its a dictionary of something? is it an assortment of different data types? –
Bobsled
It is a list containing string data type –
Milklivered
each string is a correctly formatted csv line?, could you add an example of such a string to your question? –
Bobsled
Simplest and easy way to convert PKL file to csv
import pickle as pkl
import pandas as pd
with open("file.pkl", "rb") as f:
object = pkl.load(f)
df = pd.DataFrame(object)
df.to_csv(r'file.csv')
I have found a similar question here:
How can I pickle a python object into a csv file?
You need this example from there:
import base64, csv
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])
I modify it to read from your pickle file:
import pickle
import base64
import csv
your_pickle_obj = pickle.loads(open('data.pkl', 'rb').read())
with open('output.csv', 'a', encoding='utf8') as csv_file:
wr = csv.writer(csv_file, delimiter='|')
pickle_bytes = pickle.dumps(your_pickle_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])
this is the wrong way, he wants to turn a pickle into a csv, not a csv into a pickle –
Bobsled
It is from pickle to csv :) –
Disperse
But ok, I edit this example to load the pickle file itself too. –
Disperse
Could you upload somewhere the pkl file to test it? –
Disperse
I have searched on the google a little bit about this error message. Maybe it is an encoding problem. Some people wrote a solution which worked for them. They used pickle.loads indstead of pickle.load, so try it please: your_pickle_obj = pickle.loads(open('data.pkl', 'rb').read()) –
Disperse
after writing pickle.loads, it shows error as -- "a bytes-like object is required, not '_io.BufferedReader'". please help –
Milklivered
© 2022 - 2024 — McMap. All rights reserved.