2015-10-28 122 views
1

我使用pyowm模块从OpenWeatherMap中获取天气数据。它工作得很好,但是当我请求天气,它输出它以这种格式如何隔离字符串的某个部分并将其打印出来?

<pyowm.webapi25.weather.Weather - reference time=2015-10-28 18:01:16+00, status=rain> 

是有一个,例如,抢词“雨”或“阴”,由“=之间分配的信“并关闭标签(>)到一个新的变量,然后打印这个新的变量?

回答

0
import re 
x="<pyowm.webapi25.weather.Weather - reference time=2015-10-28 18:01:16+00, status=rain>" 
print re.findall(r"(?<==)[^=]*(?=>)",x) 

您可以使用relookbehind这一点。

0

你可以通过调用找到“=”和“>”中最后一次出现的索引:

lastEqualIndex = weatherString.rfind("=") 
lastLTIndex = weatherString.rfind(">") 

那么你想要的只是:

weather = weatherString[lastEqualIndex + 1, lastLTIndex] 
2

你不需要解析字符串以获取所需的数据。您目前拥有的是pyowm.webapi25.weather.Weather类型的对象。您可以访问它的字段像这样:(其中weather_obj是你的对象的名称)

print weather_obj.get_reference_time() 
print weather_obj.get_status() 

的的documentation了解更多信息。

相关问题