Reputation: 162
I'm trying to port some code from Python 2 to 3, but it's throwing a UnicodeDecodeError
when it reads the Google API .p12 service key.
with open('service_key.p12', 'r') as f:
private_key = f.read()
Here is the error
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x82 in position 1: invalid start byte
This code works fine in python 2.7, it breaks with python 3.4
Also I have pyOpenSSL==0.14
installed.
Upvotes: 2
Views: 904
Reputation: 1021
In python 3 you have to use the 'b' flag as well for opening binary files:
with open('service_key.p12', 'rb') as f:
private_key = f.read()
As described here, Binary I/O will not do character encoding/decoding. You get the UnicodeDecodeError because you used Text I/O, which tries to decode the data as UTF-8, but your data is not valid UTF-8.
Upvotes: 5