Python 2.7 Unicode和urllib.open
我正在用python创建一个应用程序,可以从python中解析天气数据。它适用于常规ASCII字符串,但在使用unicode时失败Python 2.7 Unicode和urllib.open,python-2.7,unicode,urllib,Python 2.7,Unicode,Urllib,我正在用python创建一个应用程序,可以从python中解析天气数据。它适用于常规ASCII字符串,但在使用unicode时失败 def GetYRNOWeatherData(country, province, place): #Parse the XML file wtree = ET.parse(urllib.urlopen("http://www.yr.no/place/" + string.replace(country, ' ', '_').encode('ut
def GetYRNOWeatherData(country, province, place):
#Parse the XML file
wtree = ET.parse(urllib.urlopen("http://www.yr.no/place/" + string.replace(country, ' ', '_').encode('utf-8') + "/" + string.replace(province, ' ', '_').encode('utf-8') + "/" + string.replace(place, ' ', '_').encode('utf-8') + "/forecast.xml"))
例如,当我尝试
GetYRNOWeatherData("France", "Île-de-France", "Paris")
我得到这个错误
'charmap' codec can't encode character u'\xce' in position 0: character maps to <undefined>
“charmap”编解码器无法对位置0中的字符u'\xce'进行编码:字符映射到
urllib确实不能很好地处理unicode吗?由于我使用Tkinter作为此函数的前端,这是否是问题的根源(Tkinter条目小部件是否能够很好地处理unicode?您可以通过将每个字符串保持为
unicode
,直到您实际发出urllib.urlopen
请求,此时,您将编码
到utf-8
:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# This import makes all literal strings in the file default to
# type 'unicode' rather than type 'str'. You don't need to use this,
# but you'd need to do u"France" instead of just "France" below, and
# everywhere else you have a string literal.
from __future__ import unicode_literals
import urllib
import xml.etree.ElementTree as ET
def do_format(*args):
ret = []
for arg in args:
ret.append(arg.replace(" ", "_"))
return ret
def GetYRNOWeatherData(country, province, place):
country, province, place = do_format(country, province, place)
url = "http://www.yr.no/place/{}/{}/{}/forecast.xml".format(country, province, place)
wtree = ET.parse(urllib.urlopen(url.encode('utf-8')))
return wtree
if __name__ == "__main__":
GetYRNOWeatherData("France", "Île-de-France", "Paris")
@对不起,我在创建
url
时颠倒了省
和地方
的位置。如果你把它们调回原处,它应该可以正常工作(我只是在编辑答案时这样做的)。是的,我也注意到了这一点。谢谢