Skip to content

Instantly share code, notes, and snippets.

@skitazaki
Created November 17, 2014 14:01
Show Gist options
  • Save skitazaki/988f29a5dd0db0d2e25b to your computer and use it in GitHub Desktop.
Save skitazaki/988f29a5dd0db0d2e25b to your computer and use it in GitHub Desktop.
See [encoding - Python 3, let json object accept bytes or let urlopen output strings - Stack Overflow](http://stackoverflow.com/questions/6862770/python-3-let-json-object-accept-bytes-or-let-urlopen-output-strings)
#!/usr/bin/env python3
# See:
# encoding - Python 3, let json object accept bytes or let urlopen output strings - Stack Overflow
# http://stackoverflow.com/questions/6862770/python-3-let-json-object-accept-bytes-or-let-urlopen-output-strings
import json
import urllib.request
REMOTE_URL = 'http://data.okfn.org/data/cpi/datapackage.json'
res = urllib.request.urlopen(REMOTE_URL)
# Set default encoding as utf-8.
encoding = 'utf-8'
# Detect content-type header and extract charset value if exists.
content_type = res.getheader('content-type')
for kv in map(lambda t: t.strip().split('='), content_type.split(';')):
if len(kv) == 2 and kv[0] == 'charset':
encoding = kv[1]
# Load response data as JSON.
obj = json.loads(res.read().decode(encoding))
# Check the object.
print(type(obj))
print('\n'.join(sorted(obj.keys())))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment