我希望访问'elevation'
的值,即1608.637939453125
,并将其存储到变量'g'
中。以下是代码
import requests, json
r = requests.get(api link)
r= json.loads(r.text)
g=r['results']['elevation']
上面的代码返回此错误
TypeError: list indices must be integers or slices, not str
这是词典数据
{
"results" : [
{
"elevation" : 1608.637939453125,
"location" : {
"lat" : 39.7391536,
"lng" : -104.9847034
},
"resolution" : 4.771975994110107
}
],
"status" : "OK"
}
答案 0 :(得分:1)
在这种情况下,elevation
是字典列表,而不是另一个字典。所以你必须像这样访问g = x['results'][0]['elevation']
的值:
for result in x['results']:
print(result)
如果结果列表中有更多条目(这是常见的),您可以像这样循环遍历它们:
@echo off
set name=Backup
for /f "tokens=1-3 delims=/:" %%a in ("%TIME%") do (set mytime=%%a.%%b,%%c)
set mytime=%mytime:~0,8%
set backupname=%name%_%date%_%mytime%
set dir1= (here comes the source directory)
set dir2= (here comes the target directory)
set countvar=1
:start
For /f "tokens=1-3 delims=/:" %%a in ("%TIME%") do (set mytime=%%a.%%b,%%c)
set mytime=%mytime:~0,8%
set backupname=%name%_%date%_%mytime%
set dir2=(here comes the target directory)\%backupname%
echo Backupordner: %backupname%
ROBOCOPY %dir1% %dir2%
if "%countvar%" == "1" (
set VarDir1=%dir2%
)
if "%countvar%" == "2" (
set VarDir2=%dir2%
)
if "%countvar%" == "3" (
set VarDir3=%dir2%
rmdir /S /Q "%VarDir1%"
set VarDir1=%VarDir2%
set VarDir2=%VarDir3%
set /a countvar=%countvar%-1
)
set /a countvar=%countvar%+1
@ping -n 30 localhost> nul
goto start