腌制超过1行的pkl模型文件

时间:2019-03-29 06:09:01

标签: python python-3.x pandas scikit-learn pickle

有问题的文件在使用记事本++打开时具有以下内容:

€X   Neural Networksq .€]q (X   ClassificationqKX      Team1 winq]q(X   Team1qX   Team2qe]q(X
Team1_rankqX
Team2_rankqX   Diff1q   X   Diff2q
e}q(hcpandas.core.indexes.base
_new_Index
qcpandas.core.indexes.base
Index
q

等pkl文件中上面没有显示很多符号。

以下是我尝试将文件读取/解析为可读的代码:

泡菜:

import pickle; pickle.HIGHEST_PROTOCOL
with open(r"somefile.pkl", 'rb') as f:
data = pickle.load(f)
print (data)

使用大熊猫:

import pandas as pd
up_df = pd.read_pickle(r"somefile.pkl")
print (up_df)

在两种情况下,我都得到一个str对象,该对象只写“神经网络”

我的somefile.pkl大小为63kb,显然有字节码吗? (猜测是)在其中。如何将其转换为可读的内容?

1 个答案:

答案 0 :(得分:0)

也许pickle文件附加了多个对象。尝试类似-

objects = []
with open(filename, 'rb') as f:
data = pickle.load(f)
    while True:
        try:
            objects.append(pickle.load(filename))
        except EOFError:
            break

请参阅How to read pickle file?