0

私はスタックオーバーフローが初めてです。

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メソッドを使用することを提案しinput_file()ましたが、それでもio.UnsupportedOperation: writeおそらく書き込みメソッドであるこのエラーが発生しますが、読み取りと読み取りで使用しています私はできませんどんな方法でも罰金を科します。また、次の方法でピクルファイルを読み取ろうとしました

with open ('file.pkl', 'rb') as input_file:
    new_dict = pickle.load(input_file)

そして、私はこのエラーが発生しています

DataFrame constructor not properly called!. 

この問題をどのように解決できるかについて、いくつかの親切な提案が必要ですか? このタスクを実行できる他のツールに関する提案は、かなりの価値があります。ありがとう

4

0 に答える 0