cruelcage
cruelcage

Reputation: 2094

How to handle the encoding?

import json
import urllib2
url = "http://api.douban.com/v2/book/1220562"
hdr = { 'User-Agent' : 'super happy flair bot by /u/spladug' }
req = urllib2.Request(url, headers=hdr)
html = urllib2.urlopen(req).read()
html = json.loads(html)
func_name = 'callback'
html = '{0}({1})'.format(func_name, html)
print html

Output:

callback({u'rating': {u'max': 10, u'numRaters': 310, u'average': u'7.0', u'min': 0}, u'subtitle': u'', u'pubdate': u'2005-1', u'image': u'http://img3.douban.com/mpic/s1747553.jpg', u'binding': u'\u5e73\u88c5',..............})

I want to the ouput encoded to gb2312 or something that make it better.Maybe like

callback({'rating': {'max': 10, 'numRaters': 310, 'average': '7.0', 'min': 0......})

Sample output: url

Upvotes: 0

Views: 143

Answers (2)

Mark Tolonen
Mark Tolonen

Reputation: 177674

Consider switching to Python 3, which will display the non-ASCII characters instead of Unicode codepoints. I ran your code through the 2to3.py converter available in the Python installation's Tools\Scripts directory, then added the UTF-8 decode and pretty printing:

import json
import urllib.request, urllib.error, urllib.parse
import pprint
url = "http://api.douban.com/v2/book/1220562"
hdr = { 'User-Agent' : 'super happy flair bot by /u/spladug' }
req = urllib.request.Request(url, headers=hdr)
html = urllib.request.urlopen(req).read().decode('utf8')
html = json.loads(html)
pprint.pprint(html)

Output on a suitable terminal or IDE that supports Chinese:

{'alt': 'http://book.douban.com/subject/1220562/',
 'alt_title': '',
 'author': ['[日] 片山恭一'],
 'author_intro': '',
 'binding': '平装',
 'catalog': '\n      ',
 'id': '1220562',
 'image': 'http://img3.douban.com/mpic/s1747553.jpg',
 'images': {'large': 'http://img3.douban.com/lpic/s1747553.jpg',
            'medium': 'http://img3.douban.com/mpic/s1747553.jpg',
            'small': 'http://img3.douban.com/spic/s1747553.jpg'},
 'isbn10': '7543632608',
 'isbn13': '9787543632608',
 'origin_title': '',
 'pages': '180',
 'price': '15.00元',
 'pubdate': '2005-1',
 'publisher': '青岛出版社',
 'rating': {'average': '7.0', 'max': 10, 'min': 0, 'numRaters': 310},
 'subtitle': '',
 'summary': '那一年,是听莫扎特、钓鲈鱼和家庭破裂的一年。说到家庭破裂,母亲怪自己当初没有找到好男人,父亲则认为当时是被狐狸精迷住了眼,失常的是母亲,但出问题的是父亲……。',
 'tags': [{'count': 120, 'name': '片山恭一', 'title': '片山恭一'},
          {'count': 57, 'name': '日本', 'title': '日本'},
          {'count': 50, 'name': '日本文学', 'title': '日本文学'},
          {'count': 33, 'name': '小说', 'title': '小说'},
          {'count': 31, 'name': '满月之夜白鲸现', 'title': '满月之夜白鲸现'},
          {'count': 11, 'name': '爱情', 'title': '爱情'},
          {'count': 8, 'name': '外国文学', 'title': '外国文学'},
          {'count': 7, 'name': '純愛', 'title': '純愛'}],
 'title': '满月之夜白鲸现',
 'translator': ['豫人'],
 'url': 'http://api.douban.com/v2/book/1220562'}

Upvotes: 1

Leonardo.Z
Leonardo.Z

Reputation: 9791

Your problem is not about encoding, at least, not yet.

You are converting a dict to string with '{0}({1})'.format(func_name, html).

You should use json.dumps to convert a python object to json string.

json.dumps has a parameter ensure_ascii which default value is True.It escape all non-ASCII characters in the output with \uXXXX sequences, and the result is a str instance consisting of ASCII characters only. If ensure_ascii is False, the result may contain non-ASCII characters and the return value may be a unicode instance.

json.dumps(html, ensure_ascii=False)

Upvotes: 1

Related Questions