Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Let JSON object accept bytes or let urlopen output strings

With Python 3 I am requesting a json document from a URL.

response = urllib.request.urlopen(request) 

The response object is a file-like object with read and readline methods. Normally a JSON object can be created with a file opened in text mode.

obj = json.load(fp) 

What I would like to do is:

obj = json.load(response) 

This however does not work as urlopen returns a file object in binary mode.

A work around is of course:

str_response = response.read().decode('utf-8') obj = json.loads(str_response) 

but this feels bad...

Is there a better way that I can transform a bytes file object to a string file object? Or am I missing any parameters for either urlopen or json.load to give an encoding?

like image 990
Peter Smit Avatar asked Jul 28 '11 17:07

Peter Smit


1 Answers

Python’s wonderful standard library to the rescue…

import codecs  reader = codecs.getreader("utf-8") obj = json.load(reader(response)) 

Works with both py2 and py3.

Docs: Python 2, Python3

like image 164
jbg Avatar answered Sep 16 '22 17:09

jbg