如何优雅解决python2.x的unicode编码优雅输出?
黄舟
黄舟 2017-04-18 10:20:31
0
4
864

python2.x字符编码有一个这样的问题,类似下面这样:

>>> d = {u'subType': u'\u5f55\u97f3\u5ba4\u7248',
         u'name': u'\u5468\u6770\u4f26\u7684\u5e8a\u8fb9\u6545\u4e8b'}
>>> print d
{u'subType': u'\u5f55\u97f3\u5ba4\u7248',
u'name': u'\u5468\u6770\u4f26\u7684\u5e8a\u8fb9\u6545\u4e8b'}

>>> for i in d:
...     print i
... 
subType
name

就是这样如果我想输出一个dict,为了让它不带u前缀,必须要依次遍历输出。这样简单的单层嵌套还行,对于多层嵌套实现就有点复杂了,比如下面这个dict。请问有什么优雅的方法解决这个问题?

{u'bMusic': {u'name': None, u'extension': u'mp3', u'volumeDelta': -0.000265076, u'sr': 44100, u'dfsId': 3435973841155597, u'playTime': 215146, u'bitrate': 96000, u'id': 1215010567, u'size': 2582719}, u'hearTime': 0, u'mvid': 5382080, u'hMusic': {u'name': None, u'extension': u'mp3', u'volumeDelta': -0.32, u'sr': 44100, u'dfsId': 3435973841155595, u'playTime': 215146, u'bitrate': 320000, u'id': 1215010565, u'size': 8608958}, u'disc': u'', u'artists': [{u'img1v1Url': u'http://p4.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg', u'name': u'\u5468\u6770\u4f26', u'briefDesc': u'', u'albumSize': 0, u'img1v1Id': 0, u'musicSize': 0, u'alias': [], u'picId': 0, u'picUrl': u'http://p3.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg', u'trans': u'', u'id': 6452}], u'duration': 215146, u'id': 418603077, u'album': {u'status': 3, u'blurPicUrl': u'http://p3.music.126.net/cUTk0ewrQtYGP2YpPZoUng==/3265549553028224.jpg', u'copyrightId': 1007, u'name': u'\u5468\u6770\u4f26\u7684\u5e8a\u8fb9\u6545\u4e8b', u'companyId': 0, u'description': u'', u'pic': 3265549553028224, u'commentThreadId': u'R_AL_3_34720827', u'publishTime': 1466697600007, u'briefDesc': u'', u'company': u'\u6770\u5a01\u5c14', u'picId': 3265549553028224, u'alias': [u"Jay Chou's Bedtime Stories"], u'picUrl': u'http://p3.music.126.net/cUTk0ewrQtYGP2YpPZoUng==/3265549553028224.jpg', u'artists': [{u'img1v1Url': u'http://p3.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg', u'name': u'\u5468\u6770\u4f26', u'briefDesc': u'', u'albumSize': 0, u'img1v1Id': 0, u'musicSize': 0, u'alias': [], u'picId': 0, u'picUrl': u'http://p4.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg', u'trans': u'', u'id': 6452}], u'songs': [], u'artist': {u'img1v1Url': u'http://p3.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg', u'name': u'', u'briefDesc': u'', u'albumSize': 0, u'img1v1Id': 0, u'musicSize': 0, u'alias': [], u'picId': 0, u'picUrl': u'http://p3.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg', u'trans': u'', u'id': 0}, u'type': u'\u4e13\u8f91', u'id': 34720827, u'tags': u'', u'size': 10}, u'fee': 8, u'copyright': 2, u'no': 8, u'rtUrl': None, u'ringtone': None, u'rtUrls': [], u'score': 100, u'rurl': None, u'status': 0, u'ftype': 0, u'mp3Url': u'http://m2.music.126.net/RMJR7wDullRqppBk8dhLow==/3435973841155597.mp3', u'audition': None, u'playedNum': 0, u'commentThreadId': u'R_SO_4_418603077', u'mMusic': {u'name': None, u'extension': u'mp3', u'volumeDelta': -0.000265076, u'sr': 44100, u'dfsId': 3435973841155596, u'playTime': 215146, u'bitrate': 160000, u'id': 1215010566, u'size': 4304502}, u'lMusic': {u'name': None, u'extension': u'mp3', u'volumeDelta': -0.000265076, u'sr': 44100, u'dfsId': 3435973841155597, u'playTime': 215146, u'bitrate': 96000, u'id': 1215010567, u'size': 2582719}, u'copyrightId': 1007, u'name': u'\u544a\u767d\u6c14\u7403', u'rtype': 0, u'crbt': None, u'popularity': 100.0, u'dayPlays': 0, u'alias': [], u'copyFrom': u'', u'position': 17, u'starred': False, u'starredNum': 0}

print d
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全員に返信(4)
伊谢尔伦

こんな感じで遊べます
jsonに変換

いいねを押す +0
小葫芦

json.dumpsを使用します。indentはインデントの距離です

リーリー
いいねを押す +0
小葫芦

repr strを書き換える

いいねを押す +0
迷茫

--coding:utf-8 --

JSONをインポートする

d= {u'サブタイプ': u'u5f55u97f3u5ba4u7248',

リーリー

re = json.dumps(d,ensure_ascii=False)
再印刷

出力
{"subType": "スタジオバージョン", "name": "ジェイ・チョウのベッドサイド・ストーリー"}

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!