python - 使用python中的日期时间键返回字典中日期时间键的值
问题描述
我有一本看起来像这样的字典 -
{
"2017-04-21T08:00:00-04:00": {
"visibility": {
"prevailing": 10.0,
"units": "SM"
},
"wind": {
"speed": 13.0,
"crosswind": 2.0,
"units": "KT"
},
"temperature": {
"value": 13.9,
"units": "C"
},
"sky": [
{
"cover": "clouds",
"type": "broken",
"height": 700.0,
"units": "FT"
}
],
"code": "201704211056Z"
},
"2017-04-21T07:00:00-04:00": {
"visibility": {
"prevailing": 10.0,
"units": "SM"
},
"wind": {
"speed": 13.0,
"crosswind": 2.0,
"units": "KT"
},
"temperature": {
"value": 13.9,
"units": "C"
},
"sky": [
{
"type": "overcast",
"height": 700.0,
"units": "FT"
}
],
"code": "201704210956Z"
}
...
}
在上述字典的键中搜索给定的日期时间后,我需要返回它的值,该值也是天气报告的字典。我在 return 语句上遇到语法错误。我的代码是 -
tOff = takeoff.isoformat() #---> takeoff is the supplied datetime value to search for in the
dictionary
if tOff in weather.keys():
return weather[tOff]. #---> I get a syntax error on this statement
else:
#tm = datetime.takeoff.timetz()
for t in weather.keys():
if max(weather[t]) < tOff:
return weather[t]
else:
return
请帮助提供一些关于我为什么会出现语法错误以及如何解决它的指导
解决方案
你有一个额外的 . 在这。
if tOff in weather.keys():
return weather[tOff]. #---> I get a syntax error on this statement
您应该删除额外的 . 请参阅下面的固定代码
if tOff in weather.keys():
return weather[tOff] #---> removed . from the return statement
推荐阅读
- javascript - 如何在 JavaScript 中将用户输入转换为大写?
- php - 从不同的 Docker 容器(相同的主机,Apache)调用 Docker 容器中的 API
- python - 未来如何使用训练有素的模型
- python - 以 JSON 文件格式读取 ASN 数据
- php - Gmail 缓存搞砸了像素跟踪
- databricks - Databricks - CSV 未正确加载
- java - 在 Guice 中初始化 Java 实例时出现异常
- split - 如何获取由特定字符分隔的字符串的一部分?
- wso2-am - 登录 WSO2 API Manager 2.0.0 Carbon 控制台后显示“403 禁止”
- javascript - Javascript 功能在主页上运行,但在第二页上停止