Hello all. I’m (primarily) a Java guy learning Python and trying to wrap my head around something here. For the below API call I’m wondering why o.read() is not acceptable in place of the raw byte variable here, even when wrapped.
-Can someone help me here? Why would this be so?
Thanks!
import urllib.request
import json
o = urllib.request.urlopen(
urllib.request.Request("http://globalmentoring.com.mx/api/personas.json", data=None, headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}
)
)
raw = o.read()
print(type(o.read())) #bytes
print(type(raw)) #bytes
json.loads(raw.decode("utf-8")) #works
json.loads(o.read().decode("utf-8")) #boom
json.loads((o.read()).decode("utf-8")) #boom