通过 GET 请求,我从 Google 地理编码 API 中提取 json:
import urllib, urllib2
url = "http://maps.googleapis.com/maps/api/geocode/json"
params = {'address': 'ivory coast', 'sensor': 'false'}
request = urllib2.Request(url + "?" + urllib.urlencode(params))
response = urllib2.urlopen(request)
st = response.read()
结果看起来像:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Côte d'Ivoire",
"short_name" : "CI",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Côte d'Ivoire",
"geometry" : { ... # rest snipped
如您所见,国家名称存在一些编码问题。我试图猜测这样的编码:
import chardet
encoding = chardet.detect(st)
print "String is encoded in {0} (with {1}% confidence).".format(encoding['encoding'], encoding['confidence']*100)
返回:
String is encoded in GB2312 (with 99.0% confidence).
我想知道的是如何将其转换为具有ô
正确显示(带抑扬符的o)编码的字典。
我试过:
st = st.decode(encoding['encoding']).encode('utf-8')
但后来我得到:
{
"results" : [
{
"address_components" : [
{
"long_name" : "C么te d'Ivoire",
"short_name" : "CI",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "C么te d'Ivoire",
"geometry" : { ... # rest snipped