我正在处理一个需要大数据的Python文件,但我无法编辑我的数据。请问有人可以建议我如何将.pkl文件转换成.csv文件的代码吗?
将PKL文件转换为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')
我在这里找到了一个类似的问题:
你需要那里面的这个示例:
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])
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])
your_pickle_obj = pickle.loads(open('data.pkl', 'rb').read())
- user6035995