我有一个pickle文件需要转换为json格式,我使用了这段代码
import pickle
import pandas as pd
from sys import argv
script, filename = argv
input_file = open('f.pkl', 'rb')
new_dict = pickle.load(input_file)
df = pd.DataFrame.from_dict(new_dict, orient='index')
df['index1'] = df.index
index1 = df['index1']
df.drop(labels=['index1'], axis=1, inplace=True)
df.insert(0, 'index1', index1)
json_df = df.to_json(orient='values', date_format='iso', date_unit='s')
with open('data.json', 'w') as js_file:
js_file.write(json_df)
但是得到了
df = pd.DataFrame.from_dict(new_dict, orient='index')
File "pandas/core/frame.py", line 1127, in from_dict
if isinstance(list(data.values())[0], (Series, dict)):
AttributeError: 'list' object has no attribute 'values'
行 df = pd.DataFrame.from_dict(new_dict, orient='index') 是错误。根据错误,您尝试从 pickle 文件加载,该文件是一个列表,而不是字典:
new_dict = pickle.load(input_file)
的结果是一个列表(它可以是任何类型的数据结构)。尝试添加 isinstance()
来检查 unpickled 的数据是列表还是字典。
例如我的是 dataFrame,为此我在 Python 中使用了以下代码:
import json
import pickle
import pandas as pd
# opens the pickle file
with open('changewithyours.pickle', 'rb') as input_file:
# loads the pickle file into a pandas DataFrame
data = pd.read_pickle(input_file)
# resets the index of the DataFrame
data.reset_index(drop=True, inplace=True)
# converts the DataFrame to JSON and write it to a file
data.to_json('data.json', orient='columns')