Python Unicode字典到Twitch流中的字符串

CvR_XX

我正在尝试在python中解码抽搐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}]

我该如何解码,以便可以将其用作普通的字符串字典。

提前致谢!

马丁·彼得斯(Martijn Pieters)

您不必解码任何东西;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对象时,您不使用字符串时间戳;如果尝试进行数值计算,而是将其转换为intorfloatdecimal.Decimal()值,则不使用字节字符串

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章