json file =
{
"success": true,"terms": "https://curr
"privacy": "https://cu
"timestamp": 162764598
"source": "USD","quotes": {
"USDIMP": 0.722761,"USDINR": 74.398905,"USDIQD": 1458.90221
}
}
json 文件在上面。我从 json 中删除了很多值,因为它占用了太多空间。我的python代码在下面。
import urllib.request,urllib.parse,urllib.error
import json
response = "http://api.currencylayer.com/live?access_key="
api_key = "42141e*********************"
parms = dict()
parms['key'] = api_key
url = response + urllib.parse.urlencode(parms)
mh = urllib.request.urlopen(url)
source = mh.read().decode()
data = json.loads(source)
pydata = json.dumps(data,indent=2)
print("which curreny do you want to convert USD to?")
xm = input('>')
print(f"Hoe many USD do you want to convert{xm}to")
value = input('>')
fetch = pydata["quotes"][0]["USD{xm}"]
answer = fetch*value
print(fetch)
--------------------------------
这里是
输出
"fetch = pydata["quotes"][0]["USD{xm}"]
类型错误:字符串索引必须是整数"