python字符串索引必须是整数_TypeError:在Python中,字符串索引必须是整数
我想從currency converter API得到的Json響應(yīng)中獲取key和value,得到一個(gè)錯(cuò)誤:“of string index must be integers”。下面是Python代碼、Json中的數(shù)據(jù)和錯(cuò)誤消息。在import json
from urllib.request import urlopen
with urlopen ("http://free.currencyconverterapi.com/api/v6/convert?q=ZAR_GBP,ZAR_USD")as response:
source=response.read()
data= json.loads(source)
data=json.dumps(data,indent=2)
print(data)
value= (data['results']['ZAR_GBP']['val'])
print(value)
Json輸出為:
^{pr2}$
如果我想訪問鍵“val”及其值,這會(huì)給我一個(gè)錯(cuò)誤---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
5 data= json.loads(source)
6 data=json.dumps(data,indent=2)
----> 7 print (data['results']['ZAR_GBP']['val'])
8
TypeError: string indices must be integers
總結(jié)
以上是生活随笔為你收集整理的python字符串索引必须是整数_TypeError:在Python中,字符串索引必须是整数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: python3远程连接_Python3
- 下一篇: 笔记本中G-Sensor(加速计) M-
