我有一个如下所示的JSON数据集:
{"sequence":109428985,"bids":[["0.1243","53",5],["0.12429","24",2],["0.12428","6",1],["0.12427","6",2],["0.12426","6",1],["0.12425","6",1],["0.12424","6",1],["0.12423","6",1],["0.12422","6",1],["0.12421","6",1],["0.124206","6496",2],["0.124205","36032",1],["0.124201","20",1],["0.1242","191",2],["0.124193","400",1],["0.12419","6",1],["0.124189","1214",1],["0.12418","6",1],["0.12417","6",1],["0.12416","6",1],["0.12415","6",1],["0.12414","6",1],["0.12413","6",1],["0.12412","6",1],["0.12411","6",1],["0.1241","6",1],["0.12409","6",1],["0.12408","6",1],["0.12407","6",1],["0.12406","6",1],["0.12405","6",1],["0.124044","14",1],["0.12404","6",1],["0.12403","6",1],["0.12402","6",1],["0.12401","6",1],["0.124","64576",5],["0.12399","6",1],["0.12398","6",1],["0.12397","6",1],["0.12396","6",1],["0.12395","6",1],["0.12394","6",1],["0.12393","6",1],["0.12392","6",1],["0.12391","6",1],["0.1239","6",1],["0.12389","6",1],["0.12388","6",1],["0.12387","6",1]],"asks":[["0.124304","20",1],["0.12434","6",1],["0.12435","6",1],["0.12436","6",1],["0.12437","6",2],["0.12438","6",2],["0.12439","7",2],["0.124453","20",1],["0.124481","1",1],["0.124559","535",1],["0.12456","1210",1],["0.124566","10058",1],["0.124601","7480",1],["0.12462","19",2],["0.124621","1",1],["0.12463","6",1],["0.12464","6",1],["0.12465","6",1],["0.12466","6",1],["0.124668","5306",1],["0.124669","1",1],["0.12467","6",1],["0.124671","20",1],["0.124674","691",1],["0.12468","6",1],["0.124683","20",1],["0.12469","6",1],["0.124697","20",1],["0.1247","6",1],["0.12471","6",1],["0.12472","6",1],["0.12473","6",1],["0.12474","6",1],["0.12475","6",1],["0.12476","6",1],["0.12477","7",2],["0.124779","20",1],["0.12478","7",2],["0.124784","12",1],["0.12479","6",1],["0.124796","1",1],["0.1248","6",1],["0.12481","6",1],["0.12483","6",1],["0.12484","6",1],["0.12485","6",1],["0.124855","1",1],["0.12487","6",1],["0.124889","16500",1],["0.12489","6",1]]}
我试图从列表的前两个元素中提取数据,并进行更改,以便可以将它们作为数字放入数据库中。
例如,此:
["0.1243","53",5],["0.12429","24",2],["0.12428","6",1]
应更改为此:
0.1243 53
0.12429 24
0.12428 6
这是我当前的代码:
import requests
import json
import re
url = 'https://api.pro.coinbase.com/products/BAT-USDC/book?level=2'
trade_data = requests.get(url).json()
bid_data = trade_data['bids']
print(bid_data)
这是返回的内容:
[[u'0.124456', u'1158', 3], [u'0.12445', u'6', 1], [u'0.12442', u'6', 1], [u'0.12441', u'6', 1], [u'0.1244', u'6', 1], [u'0.12439', u'6', 1], [u'0.12438', u'6', 1], [u'0.12437', u'6', 2], [u'0.12436', u'6', 2], [u'0.12435', u'6', 1], [u'0.12434', u'6', 1], [u'0.12433', u'6', 1], [u'0.124315', u'6991', 1], [u'0.124314', u'1212', 1], [u'0.1243', u'6', 3], [u'0.12429', u'6', 2], [u'0.12428', u'6', 1], [u'0.12427', u'6', 2], [u'0.124261', u'2419', 1], [u'0.12426', u'6', 1], [u'0.124251', u'20', 1], [u'0.12425', u'6', 1], [u'0.12424', u'6', 1], [u'0.12423', u'6', 1], [u'0.12422', u'6', 1], [u'0.12421', u'6', 1], [u'0.124206', u'20', 1], [u'0.124205', u'36032', 1], [u'0.124201', u'20', 1], [u'0.1242', u'391', 3], [u'0.124193', u'400', 1], [u'0.12419', u'6', 1], [u'0.12418', u'6', 1], [u'0.12417', u'6', 1], [u'0.12416', u'6', 1], [u'0.12415', u'6', 1], [u'0.12414', u'6', 1], [u'0.12413', u'6', 1], [u'0.12412', u'6', 1], [u'0.12411', u'6', 1], [u'0.1241', u'6', 1], [u'0.12409', u'6', 1], [u'0.12408', u'6', 1], [u'0.12407', u'6', 1], [u'0.12406', u'6', 1], [u'0.12405', u'6', 1], [u'0.124044', u'14', 1], [u'0.12404', u'6', 1], [u'0.12403', u'6', 1], [u'0.12402', u'6', 1]]
我不知道u的来源,也不知道如何从这种“列表中列出”格式中分离出对我的计算重要的2个数字。我是一个编程新手,对此我深表歉意。
我们非常感谢您的帮助,在此先感谢您!
我找到的最近的上一个问题:
how to extract string inside single quotes using python script
答案 0 :(得分:0)
library(gtools)
x <- c(TRUE, FALSE)
permutations(n=length(x),r=2,v=x,repeats.allowed=T)
前缀只是意味着
您要做的只是简单地遍历列表并转换您要浮动的内容:
u
答案 1 :(得分:0)
在处理所谓的 list-comprehensions 列表很方便时,例如:
data = [["0.1243","53",5],["0.12429","24",2],["0.12428","6",1]]
firsttwo = [i[:2] for i in data]
numbers = [[float(i[0]),int(i[1])] for i in firsttwo]
print(numbers) #prints [[0.1243, 53], [0.12429, 24], [0.12428, 6]]
请注意,这假定第一个数字始终为浮点数,第二个数字为整数。
i[:2]
是切片的用法,可以理解为:在第二个元素之后剪切列表并获得“左”部分(即剪切之前)。
您可以在此处阅读有关列表理解的更多信息:https://www.pythonforbeginners.com/basics/list-comprehensions-in-python