0
我正在python中創建一個應用程序,它可以在Python中解析來自yr.no的天氣數據。它可以正常使用ASCII字符串,但在使用unicode時失敗。Unicode和urllib.open
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>
難道urllib的不處理Unicode的很好?由於我使用的Tkinter作爲一個前端到這個功能,將是這個問題的來源(是否Tkinter的輸入控件處理Unicode呢?)
@Igor對不起,我已經創建'url'時逆轉province'和'place'的'位置。它應該正常工作,如果你將它們交換回來(我只是在編輯答案時這樣做)。 – dano
是的,我也注意到了。謝謝 – Igor