如何将值压缩到一个列表不均匀的表中? (DataNitro)

我试图通过各自的JSON API从货币交易所获得最后的5个订单。 一切正常,除了事实上有一些硬币有less于5个订单(问/投标),导致表中的一些错误写入Excel。

这是我现在拥有的:

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) 

在terminal里打印出来很好:

itertoolsterminal输出

注意:截至目前为止,“REE”的出价单为零,因此最终创build了一个空列表:

空的列表terminal输出

当打印出色,我得到了很多奇怪的输出。 没有一个类似于terminal中的外观。 在Excel中设置信息的方式要求它是Cell(X,X).table

我的问题是,如何使用不均匀的列表进行压缩,在DataNitro中使用表格进行更好的播放?

编辑1:问题出现在catalog_response.json()[name]

 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 

由于出价为零,甚至没有创build空列表,这就是为什么我无法将它们压缩在一起。 ValueError:需要多个值才能解包

我build议你build立你打算写回excel的结构myTable。 它应该是一个列表清单

 myTable = [] myRow = [] 

…从您的代码构build每个myRow …如果myRow列表的长度太短,请在您的情况下填充适当数量的[None]元素,如果len(myRow)为0,则需要附加两个“None”项

 myRow.append(None) myRow.append(None) 

将该行添加到输出表

 myTable.append(myRow) 

所以当准备好时,你有一个良好的nn xn表输出通过:

 Cell(nn, n).table = myTable