简体   繁体   中英

Pickle-loading more than 1 line of pkl model file

File in question has lines like this when opened with Notepad++:

€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

etc. There are a lot of symbols that aren't shown above in the pkl file.

Below are the codes I've tried to read/parse the file into something readable:

With pickle:

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

With pandas:

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

In both cases, I get a str object that only writes 'Neural Network'

My somefile.pkl is 63kb in size and obviously has bytecode? (guessing it is) in it. How do I go about converting it to something readable?

Maybe the pickle file has multiple objects appended to it. Try something like -

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

Refer to How to read pickle file?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM