1

我正在尝试通过他们各自的 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)

在终端中很好地打印出来:

itertools 终端输出

注意:截至目前,“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 个值才能解包

4

1 回答 1

0

我建议您构建您打算写回 excel 的结构 myTable。它应该是一个列表列表

myTable = []
myRow = []

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

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

将行添加到输出表

myTable.append(myRow)

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

Cell(nn, n).table = myTable
于 2017-09-22T20:40:33.430 回答