如何将值压缩到具有不均匀列表的表中?(数据硝基)

2024-04-26 19:03:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过各自的jsonapi从货币交易所获得最后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 terminal output

注意:到目前为止,“REE”没有出价顺序,因此它最终会创建一个空列表:

empty list terminal output

当打印到excel时,我得到了很多奇怪的输出。它们都不像候机楼里的样子。在Excel中设置信息的方式要求它是Cell(X,X).table

我的问题是,如何使不均匀列表的压缩在DataNitro中的表中发挥良好的效果?你知道吗

编辑1: 问题出现在目录上_响应.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个以上的值才能解包


Tags: nameinjsonformakeresponsetablecell
1条回答
网友
1楼 · 发布于 2024-04-26 19:03:56

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

myTable = []
myRow = []

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

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

将行添加到输出表

myTable.append(myRow)

因此,当准备就绪时,您将有一个格式良好的nn x n表通过以下方式输出:

Cell(nn, n).table = myTable

相关问题 更多 >