如何将值压缩到具有不均匀列表的表中? (DataNitro)
问题描述:
我试图通过各自的JSON API从货币兑换中获取最后5个订单。除了事实上有一些硬币的订单数少于5(询问/出价),这会导致表格中的一些错误写入Excel,所有事情都在起作用。如何将值压缩到具有不均匀列表的表中? (DataNitro)
这是我现在有:
import grequests
import json
import itertools
active_sheet("Livecoin Queries")
urls3 = [
'https://api.livecoin.net/exchange/order_book?
currencyPair=RBIES/BTC&depth=5',
'https://api.livecoin.net/exchange/order_book?
currencyPair=REE/BTC&depth=5',
]
requests = (grequests.get(u) for u in urls3)
responses = grequests.map(requests)
CellRange("B28:DJ48").clear()
def make_column(catalog_response, name):
column = []
catalog1 = catalog_response.json()[name]
quantities1, rates1 = zip(*catalog1)
for quantity, rate in zip(quantities1, rates1):
column.append(quantity)
column.append(rate)
return column
bid_table = []
ask_table = []
for response in responses:
try:
bid_table.append(make_column(response,'bids'))
ask_table.append(make_column(response,'asks'))
except (KeyError,ValueError,AttributeError):
continue
Cell(28, 2).table = zip(*ask_table)
Cell(39, 2).table = zip(*bid_table)
我孤立的链接列表下降到只有两带“REE”硬币是这里的问题。
我已经试过:
for i in itertools.izip_longest(*bid_table):
#Cell(28, 2).table = zip(*ask_table)
#Cell(39, 2).table = zip(*i)
print(i)
其中在终端很好地打印出:
注:由于现在“稀土元素”的具有零级投标的订单,因此结束了创建一个空列表:
当打印出色,我得到了很多奇怪的输出。没有一个与终端中的外观相似。在Excel中设置信息的方式要求它是单元格(X,X)。表格
我的问题是,如何使用不均匀列表进行压缩以便与DataNitro中的表格相处得很好?
EDIT1: 问题是在catalog_response.json产生()[名]
def make_column(catalog_response, name):
column = []
catalog1 = catalog_response.json()[name]
#quantities1, rates1 = list(itertools.izip_longest(*catalog1[0:5]))
print(catalog1)
#for quantity, rate in zip(quantities1, rates1):
# column.append(quantity)
# column.append(rate)
#return column
既然有零个报价甚至没有创建一个空列表这就是为什么我无法压缩它们一起。 ValueError异常:需要比0值更解压
答
我建议你建立你打算写回Excel结构myTable的。 应该列出
myTable = []
myRow = []
列表...从您的代码构建每个myRow ... 如果列表myRow的长度过短,垫[无]元素 在你的情况下,如果适当的数len(myRow)
为0,你需要追加两个“无”项目
myRow.append(None)
myRow.append(None)
行添加到输出表
myTable.append(myRow)
所以准备当你有一个很好经形成NN×n的表输出:
Cell(nn, n).table = myTable