Created
November 17, 2014 14:01
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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