我是堆栈溢出的新手。
我正在尝试使用 python 将 pkl 文件转换为 json 文件。下面是我的示例代码
import pickle
import pandas as pd
# Load pickle file
input_file = open('file.pkl', 'rb')
new_dict = pickle.load(input_file)
input_file()
# Create a Pandas DataFrame
data_frame = pd.DataFrame(new_dict)
# Copy DataFrame index as a column
data_frame['index'] = data_frame.index
# Move the new index column to the from of the DataFrame
index = data_frame['index']
data_frame.drop(labels=['index'], axis=1, inplace = True)
data_frame.insert(0, 'index', index)
# Convert to json values
json_data_frame = data_frame.to_json(orient='values', date_format='iso', date_unit='s')
with open('data.json', 'w') as js_file:
js_file.write(json_data_frame)
当我运行这段代码时,我得到了错误TypeError: '_io.TextIOWrapper' object is not callable
。通过遵循一些相同的问题This one和This one,这些问题建议在第 7 行使用write
方法 withinput_file()
但我仍然收到此错误io.UnsupportedOperation: write
,这可能是一种写入方法,但我将它用于阅读和阅读我无法罚款任何方法。我也尝试通过以下方式读取泡菜文件
with open ('file.pkl', 'rb') as input_file:
new_dict = pickle.load(input_file)
我收到了这个错误
DataFrame constructor not properly called!.
我需要一些好的建议来解决这个问题?有关可以执行此任务的其他工具的任何建议,都将是可观的。谢谢