How to open a .pkl file and view the contents of the .pkl file (Python3.6)

 1. show_pkl.py:

#show_pkl.py

import pickle
path='aus_openface.pkl'   #path='/root/……/aus_openface.pkl'   pkl文件所在路径
	   
f=open(path,'rb')
data=pickle.load(f)

print(data)
print(len(data))

#输出结果:
{'N_0000000356_00190': array([2.86, 2.27, 1.45, 1.1 , 0.  , 0.65, 0.05, 0.  , 0.75, 1.65, 0.6 , 0.  , 1.86, 0.  , 0.62, 0.25, 0.  ]),
 'N_0000001939_00054': array([0.34, 2.09, 0.  , 2.04, 0.02, 0.  , 0.  , 1.22, 0.  , 0.93, 0.37, 0.  , 0.4 , 0.  , 0.  , 0.22, 0.  ]),
 'N_0000000437_00540': array([0.  , 0.19, 0.02, 0.8 , 0.24, 1.46, 1.18, 0.37, 0.  , 0.  , 1.13, 3.37, 1.24, 0.73, 0.13, 1.83, 0.  ]),
 'N_0000001507_00202': array([1.08, 1.23, 0.  , 1.83, 0.31, 1.08, 0.04, 0.  , 0.24, 1.31, 0.  , 0.25, 0.44, 0.6 , 0.77, 0.  , 0.  ])}
4

2. Run the command: python show_pkl.py. The result is as shown in the figure below, and you can see the contents of the .pkl file:

(Note: The file show_pkl.py is saved in the F:\learn_pytorch directory, and aus_openface.pkl is also saved in the F:\learn_pytorch directory)

 

3. Special circumstances

Sometimes when downloading foreign code, you need to open the pkl file in the code. The encoding method of foreigners writing code may be different from ours. For example, I have encountered that using the above code, I still cannot open the pkl file given by foreigners.

报错:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe1 in position 0: ordinal not in range(128)

Solution: Add encoding,  data=pickle.load(f,encoding= 39;latin1') , successfully solved my problem.

 

参考:https://blog.csdn.net/LemonTree_Summer/article/details/81906547

Guess you like

Origin blog.csdn.net/weixin_39450145/article/details/104897934