2014-11-20 56 views
-1

我試圖從一個城鎮獲取天氣信息,並從該列表中打印出2個項目。然而,當我嘗試運行代碼,我得到這個錯誤:AtrributeError:'NoneType'對象沒有屬性'data'&TypeError:'str'對象不可調用

Traceback (most recent call last): 
    File "/home/pi/Desktop/python/PyWeather.py" line 4, in <module> 
     bostonweather = pywapi.get_weather_from_weather_com('UKXX1701', units = 'metric') 
    File "/home/pi/Desktop/python/pywapi.py" line 239, in get_weather_from_weather_com 
     'wind')[0].getElementsByTagName(tag2)[0].firstChild.data 
AtrributeError: 'NoneType' object has no attribute 'data' 

這是我的代碼:

import pywapi 
import string 

bostonweather = pywapi.get_weather_from_weather_com('UKXX1701', 'metric') 
print ("In Boston, Lincolnshire it is currently: ") + string.ascii_lowercase(bostonweather['current_conditions']['text'] + (" and ") + string.ascii_lowercase(bostonweather['current_conditions']['temperature'] + ("C.\n")) 

//編輯

我剛剛重新運行了一個程序,它只是拋出一個「TypeError:'str'對象不可調用」的錯誤。
錯誤消息:

Traceback (most recent call last): 
    File "/home/pi/Desktop/python/PyWeather.py" line 5, in <module> 
     print ("In Boston, Lincolnshire it is currently: ") + string.ascii_lowercase(bostonweather['current_conditions']['text'] + (" and ") + string.ascii_lowercase(bostonweather['current_conditions']['temperature'] + ("C.\n")) 
TypeError: 'str' object is not callable 

做什麼任何線索?

回答

0

嘗試打印

import pywapi 

bostonweather = pywapi.get_weather_from_weather_com('UKXX1701', 'metric') 
print ("In Boston, Lincolnshire it is currently: " + str(bostonweather['current_conditions']['text']).lower() + " and " + str(bostonweather['current_conditions']['temperature']).lower()) 
+0

這仍然給我一個Typeerror:不支持的操作數蘭德類型的+:'NoneType'和'str'' :( 編輯:沒關係!我從你的代碼中刪除了一些括號,並且它工作:)謝謝! – davidpox 2014-11-22 16:39:51

+0

不知道爲什麼它會給你錯誤,我用python3.4檢查過,它很好。我想你的翻譯有不同的版本,但無論如何,我很高興你修復它:) – 2014-11-23 22:06:29

0

你試圖調用string.lowercase(blabla)而這也正是你TypeError

你應該做

print "In Boston, Lincolnshire it is currently: " + bostonweather['current_conditions']['text'].lower() + " and " + bostonweather['current_conditions']['temperature'].lower() + ("C.\n") 
+0

之前,我想這讓所有的字符串,但現在我得到這個錯誤: 'typeerror:不支持的操作數類型爲+:'NoneType'和'str'' – davidpox 2014-11-20 23:39:59