-1

Pythonでtwitch APIの回答をデコードしようとしています。

import urllib2
import json


url  = 'http://api.justin.tv/api/stream/list.json?channel=kungentv'

result =json.loads(urllib2.urlopen(url, timeout = 100).read().decode('utf-8'))
print result

これを実行すると、次のようになります。

[{u'broadcast_part': 6, u'featured': True, u'channel_subscription': True, u'embed_count': 0, u'id': u'9602378624', u'category': u'gaming', u'title': u'5x Legend - Playing the TOP DECKS from DECK WARS to Legendary! Handlock, Miracle and Shaman. (Never played Shaman EVUR)', u'video_height': 1080, u'site_count': 0, u'embed_enabled': False, u'channel': {u'category': u'gaming', u'status': u'5x Legend - Playing the TOP DECKS from DECK WARS to Legendary! Handlock, Miracle and Shaman. (Never played Shaman EVUR)', u'views_count': 56403101, u'subcategory': None, u'language': u'en', u'title': u'kungentv', u'screen_cap_url_huge': u'http://static-cdn.jtvnw.net/previews/live_user_kungentv-630x473.jpg', u'producer': True, u'tags': None, u'subcategory_title': u'', u'category_title': u'', u'screen_cap_url_large': u'http://static-cdn.jtvnw.net/previews/live_user_kungentv-320x240.jpg', u'mature': None, u'screen_cap_url_small': u'http://static-cdn.jtvnw.net/previews/live_user_kungentv-70x53.jpg', u'screen_cap_url_medium': u'http://static-cdn.jtvnw.net/previews/live_user_kungentv-150x113.jpg', u'timezone': u'Europe/Stockholm', u'login': u'kungentv', u'channel_url': u'http://www.justin.tv/kungentv', u'id': 30383713, u'meta_game': u'Hearthstone: Heroes of Warcraft'}, u'up_time': u'Mon May 19 00:34:43 2014', u'meta_game': u'Hearthstone: Heroes of Warcraft', u'format': u'live', u'stream_type': u'live', u'channel_count': 3671, u'abuse_reported': False, u'video_width': 1920, u'geo': u'SE', u'name': u'live_user_kungentv', u'language': u'en', u'stream_count': 0, u'video_bitrate': 3665.328125, u'broadcaster': u'obs', u'channel_view_count': 0}]

これをデコードして、通常の文字列辞書として使用するにはどうすればよいですか。

前もって感謝します!

4

1 に答える 1

3

何もデコードする必要はありません。Python は、必要に応じて文字列値をエンコードおよびデコードします。

デモ:

>>> result = [{u'broadcast_part': 6, u'featured': True, u'channel_subscription': True, u'embed_count': 0, u'id': u'9602378624', u'category': u'gaming', u'title': u'5x Legend - Playing the TOP DECKS from DECK WARS to Legendary! Handlock, Miracle and Shaman. (Never played Shaman EVUR)', u'video_height': 1080, u'site_count': 0, u'embed_enabled': False, u'channel': {u'category': u'gaming', u'status': u'5x Legend - Playing the TOP DECKS from DECK WARS to Legendary! Handlock, Miracle and Shaman. (Never played Shaman EVUR)', u'views_count': 56403101, u'subcategory': None, u'language': u'en', u'title': u'kungentv', u'screen_cap_url_huge': u'http://static-cdn.jtvnw.net/previews/live_user_kungentv-630x473.jpg', u'producer': True, u'tags': None, u'subcategory_title': u'', u'category_title': u'', u'screen_cap_url_large': u'http://static-cdn.jtvnw.net/previews/live_user_kungentv-320x240.jpg', u'mature': None, u'screen_cap_url_small': u'http://static-cdn.jtvnw.net/previews/live_user_kungentv-70x53.jpg', u'screen_cap_url_medium': u'http://static-cdn.jtvnw.net/previews/live_user_kungentv-150x113.jpg', u'timezone': u'Europe/Stockholm', u'login': u'kungentv', u'channel_url': u'http://www.justin.tv/kungentv', u'id': 30383713, u'meta_game': u'Hearthstone: Heroes of Warcraft'}, u'up_time': u'Mon May 19 00:34:43 2014', u'meta_game': u'Hearthstone: Heroes of Warcraft', u'format': u'live', u'stream_type': u'live', u'channel_count': 3671, u'abuse_reported': False, u'video_width': 1920, u'geo': u'SE', u'name': u'live_user_kungentv', u'language': u'en', u'stream_count': 0, u'video_bitrate': 3665.328125, u'broadcaster': u'obs', u'channel_view_count': 0}]
>>> result[0]['broadcast_part']
6

Python 2 は ASCII を使用して、必要に応じて Unicode とバイト文字列値の間でエンコード/デコードを行います。

一般的に言えば、テキストデータは Unicode 値として必要です。プログラムは Unicode サンドイッチでなければなりません。受信時にデータを Unicode にデコードし、再度送信するときにエンコードします。これは、他のシリアル化されたデータと同じです。オブジェクトを操作できる場合は文字列のタイムスタンプをdatetime操作しない、数値計算を行う場合はバイト文字列を操作しない、代わりにintorfloatまたはdecimal.Decimal()値に変換する、など。

于 2014-05-19T10:34:24.183 に答える