使用百度地图web service API时取到的数据中, 中文都是\u 这种unicode, 太坑爹了.如何将unicode转换成中文并打印或者输出到文件中呢?
在网上找了好久,终于找到了方法, 使用decode('unicode_escape')将字符串解码为unicode, 然后再用encode("UTF-8")将unicode码编码为gbk汉字码
# -*- coding: utf-8 -*-
ak = "B5f10******b28******e5****************9d221"
url = r"http://api.map.baidu.com/direction/v1?mode=driving&origin=上地五街&destination=北京大学&origin_region=北京&destination_region=北京&output=json&ak=" + ak
import urllib2
import json
# import httplib2
def getContent(url):
req = urllib2.Request(url)
req.set_proxy("siwebproxy01.apac.nokia.com:8080", "http")
res = urllib2.urlopen(req)
content = res.read()
return content
if __name__ == '__main__':
# httplib2.urllib.parse.unquote('\u6ce8\u91ca')
content = getContent(url)
cc = content.decode("unicode_escape")
ccc = cc.encode("UTF-8")
print ccc