从这里开始,希望这很简单,有人可以朝着正确的方向推动我。
我想做的是将dict中的值设置为变量,以便每次运行时都为变量分配新数据
import requests
import json
from requests.auth import HTTPBasicAuth
import datetime
import time
from datetime import datetime
weatherString = requests.get('https://twcservice.mybluemix.net/api/weather/v1/geocode/33.40/-83.42/observations.json?language=en-US&units=e', auth=HTTPBasicAuth('user','pass'))
data=weatherString.json()
result = {
"Hum": data['observation']['rh'],
"Pressure": data['observation']['pressure'],
"DewPt": data['observation']['dewPt'],
"Temp": data['observation']['temp'],
"Time": datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
}
final = json.dumps(result)
print(final)
简而言之,我希望Hum,Pressure,DewPt,Temp和Time为变量,而不是dict中的键。
答案 0 :(得分:0)
您可以直接分配给变量,该变量的值应该已经输入。
data=weatherString.json()
data = json.loads(data)
Hum = data['observation']['rh']
Pressure = data['observation']['pressure']
DewPt = data['observation']['dewPt']
Temp = data['observation']['temp']
Time = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
答案 1 :(得分:0)
如果您希望数据是“变量”而不是“键”,则可以使用类似情况我建议的DictObject,传入const words = ["something", "else", "and", "d", "more", "things", "in", "the", "d", "array", "etc"]
let grouped = words.reduce((response,word)=>{
if (word!=='d')
response[response.length-1].push(word)
else
response[response.length]=[]
return response
},[[]])
console.log(grouped)
(尽管您需要综合分别data['observation']
,您不会得到您在问题中暗指的名字。