字符串索引必须是整数,而通过JSON解析
因此,我试图抓住“ rate_float”
,但是每当通过响应运行时,我都会得到TypeError。
我很确定,当试图通过“ BPI”
我最初认为这是因为包含在字符串中,但它是浮点,这仍然是造成问题,或者我不正确解析JSON以获取“ USD”
,并获得“ rate_float”的值
对美元。
错误消息:
Traceback (most recent call last):
File "C:\Users\Blue\python_practice\problem_sets\Bitcoin_Price", line 37, in <module>
main()
File "C:\Users\Blue\python_practice\problem_sets\Bitcoin_Price", line 7, in main
bitcoin_price = bitcoin()
File "C:\Users\Blue\python_practice\problem_sets\Bitcoin_Price", line 34, in bitcoin
print(p["rate_float"])
TypeError: string indices must be integers
完整的代码:
import requests
import json
import sys
def main():
bitcoin_price = bitcoin()
print(bitcoin_price)
try:
if float(args()):
print(args())
return True
except ValueError:
print("Invalid Amount / Not a float or digit")
sys.exit()
def args():
arg = [float(x) for x in sys.argv[1:]]
user_arg = arg.pop(0)
return user_arg
def bitcoin():
response = requests.get(
"https://api.coindesk.com/v1/bpi/currentprice.json")
print(json.dumps(response.json(), indent=2))
usd_price = response.json()
for p in usd_price["bpi"]:
print(p["rate_float"])
main()
JSON:
{
"time": {
"updated": "Jun 12, 2022 23:29:00 UTC",
"updatedISO": "2022-06-12T23:29:00+00:00",
"updateduk": "Jun 13, 2022 at 00:29 BST"
},
"disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
"chartName": "Bitcoin",
"bpi": {
"USD": {
"code": "USD",
"symbol": "$",
"rate": "27,091.6395",
"description": "United States Dollar",
"rate_float": 27091.6395
},
"GBP": {
"code": "GBP",
"symbol": "£",
"rate": "21,996.2168",
"description": "British Pound Sterling",
"rate_float": 21996.2168
},
"EUR": {
"code": "EUR",
"symbol": "€",
"rate": "25,752.4997",
"description": "Euro",
"rate_float": 25752.4997
}
}
}
So, I am trying to grab the "rate_float"
, but whenever running through the response I get the TypeError.
I am pretty sure I the TypeError is actually happening right off the bat whenever trying to loop through "bpi"
I originally thought it was because contained in a string but it's a float, is that still what is causing the issue, or is that im not parsing the json correctly to get to "USD"
, and get the value pair of "rate_float"
Correct code should just output the rate_float of USD.
Error Message:
Traceback (most recent call last):
File "C:\Users\Blue\python_practice\problem_sets\Bitcoin_Price", line 37, in <module>
main()
File "C:\Users\Blue\python_practice\problem_sets\Bitcoin_Price", line 7, in main
bitcoin_price = bitcoin()
File "C:\Users\Blue\python_practice\problem_sets\Bitcoin_Price", line 34, in bitcoin
print(p["rate_float"])
TypeError: string indices must be integers
Full set of code:
import requests
import json
import sys
def main():
bitcoin_price = bitcoin()
print(bitcoin_price)
try:
if float(args()):
print(args())
return True
except ValueError:
print("Invalid Amount / Not a float or digit")
sys.exit()
def args():
arg = [float(x) for x in sys.argv[1:]]
user_arg = arg.pop(0)
return user_arg
def bitcoin():
response = requests.get(
"https://api.coindesk.com/v1/bpi/currentprice.json")
print(json.dumps(response.json(), indent=2))
usd_price = response.json()
for p in usd_price["bpi"]:
print(p["rate_float"])
main()
json:
{
"time": {
"updated": "Jun 12, 2022 23:29:00 UTC",
"updatedISO": "2022-06-12T23:29:00+00:00",
"updateduk": "Jun 13, 2022 at 00:29 BST"
},
"disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
"chartName": "Bitcoin",
"bpi": {
"USD": {
"code": "USD",
"symbol": "$",
"rate": "27,091.6395",
"description": "United States Dollar",
"rate_float": 27091.6395
},
"GBP": {
"code": "GBP",
"symbol": "£",
"rate": "21,996.2168",
"description": "British Pound Sterling",
"rate_float": 21996.2168
},
"EUR": {
"code": "EUR",
"symbol": "€",
"rate": "25,752.4997",
"description": "Euro",
"rate_float": 25752.4997
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在这里,
当您迭代字典时,您会迭代其键。这样,
p
是字符串(例如usd
,gbp
etc)。考虑使用
items()
对密钥和值进行迭代:The problem is here
When you iterate over a dictionary, you iterate over its keys. That way,
p
is a string (e.g.USD
,GBP
etc).Consider iterating over the key and values using
items()
: