Q:

How python2.7 to read load file

I use python2.7 and the script is write in python3.5,and the code is:
with open('camera.p', 'rb') as f:
     save_dict = pickle.load(f)
mtx = save_dict['mtx']
dist = save_dict['dist']

The error is:
KeyError: 'mtx'

so how to solve this problem,thank you!

A:

This is because the key mtx does not exist in the un-pickled save_dict.
If you can use a default value for mtx, you can use the dict.get function:
mtx = save_dict.get('mtx', 'my default value')

# or check if the key exists
if 'mtx' in save_dict:
    process_value(save_dict['mtx'])

I recommend opening the python repl in a terminal and inspecting the object there. For example:
# In the terminal
$ python
Python 3.6.1 (default, Mar 23 2017, 14:40:12)
[GCC ...]
Type "help", "copyright", "credits" or "license" for more information.

Now inside the python repl, you can execute your code line by line:
>>> import pickle
>>> with open('camera.p', 'rb') as f:
...     save_dict = pickle.load(f)
...
>>> type(save_dict)
<class 'dict'>
>>> save_dict.keys()
dict_keys(['foo', 'bar', 'x'])

This will give you a better sense of why the mtx key is unexpectedly missing from the save_dict.

