我正在尝试从网站检索数据: https://api.coinmarketcap.com/v1/ticker/cardano/?convert=usd
代码段如下所示:
with urllib.request.urlopen("https://api.coinmarketcap.com/v1/ticker/cardano/?convert=USD") as url:
data = json.loads(url.read().decode())
print(data)
输出为:
[{'id':'cardano'、'name':'cardano'、'symbol':'ADA'、'rank':'5'、'price\u-usd':'0.81872'、'price\u-btc':'0.00005809'、'24h\u-volume\u-usd':'213316000.0'、'market\u-cap\u-usd':'2122701191.0'、'available\u-supply':'259270538.0'、'total\u-supply':'31112483745.0'、'max\u-supply':'45000000000.0'、'percent\u-change\u-1,'百分比变化24小时':'13.13','百分比变化7天':'-19.93','上次更新':'1515768856'}]
我的问题是,我该如何处理这些文本?我能把它列成一张好看的单子吗?你知道吗
提前谢谢。你知道吗
附言:我现在正在和Python合作
要获得price\u usd元素,可以使用
data[0]['price_usd']
。你知道吗您可以使用
pprint
模块以更好的格式打印它。你知道吗我非常推荐使用
requests
库来做这类事情。它非常灵活,是一种事实上的自由库,用于处理有要求的事情。你知道吗例如(在其中我冒昧地使用了这样的lib和iPython):
如果要将响应打印为字符串,可以使用内置的lib
json
(用于将其转储到文件或其他文件中):你可以用这样的方法:
这将变成一个更容易阅读的视图。但我不确定这是否会让你更容易处理脚本中的信息。pprint是另一个很棒的模块。你知道吗
相关问题 更多 >
编程相关推荐