JSON encoding PDF or Excel files in Python 2.7

MRAB python at mrabarnett.plus.com
Fri Jul 21 15:45:29 EDT 2017


On 2017-07-21 19:52, Skip Montanaro wrote:
> I would like to JSON encode some PDF and Excel files. I can read the content:
> 
> pdf = open("somefile.pdf", "rb").read()
> 
> but now what?  json.dumps() insists on treating it as a string to be
> interpreted as utf-8, and bytes == str in Python 2.x. I can't
> json.dumps() a bytearray. I can pickle the raw content and json.dumps
> that, but I can't guarantee the listener at the other end will be
> written in Python. Am I going to have to do something like
> base64-encode the raw bytes to transmit them?
> 
JSON supports floats, ints, (Unicode) strings, lists and dicts (with 
string keys). It doesn't support bytestrings (raw bytes).

Yes, you're going to have to 'encode' it somehow into one of the 
available types.



More information about the Python-list mailing list