我正在尝试通过他们各自的 JSON API 从货币交易所获取最后 5 个订单。一切正常,除了有些硬币的订单少于 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)
在终端中很好地打印出来:
注意:截至目前,“REE”的投标订单为零,因此最终会创建一个空列表:
打印到 excel 时,我得到了很多奇怪的输出。这些都不像它在终端中的样子。在 Excel 中设置信息的方式要求它是 Cell(X,X).table
我的问题是,如何使不均匀列表的压缩与 DataNitro 中的表格配合得很好?
EDIT1:问题出现在 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
由于出价为零,甚至没有创建一个空列表,这就是我无法将它们压缩在一起的原因。 ValueError:需要超过 0 个值才能解包