2014-11-20 61 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