1

I'm attempting to get the last 5 orders from currency exchanges through their respective JSON API. Everything is working except for the fact there are some coins that have less than 5 orders (ask/bid) which causes some errors in the table write to Excel.

Here is what I have now:

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)

I've isolated the list of links down to just two with "REE" coin being the issue here.

I've tried:

for i in itertools.izip_longest(*bid_table):
    #Cell(28, 2).table = zip(*ask_table)
    #Cell(39, 2).table = zip(*i)                               
    print(i)

Which prints out nicely in the terminal:

itertools terminal output

NOTE: As of right now "REE" has zero bid orders so it ends up creating an empty list:

empty list terminal output

When printing to excel I get a lot of strange outputs. None of which resemble what it looks like in the terminal. The way the information is set up in Excel requires it to be Cell(X,X).table

My question is, how do I make zipping with uneven lists play nice with tables in DataNitro?

EDIT1: The problem is arising at 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

Since there are zero bids there is not even an empty list created which is why I'm unable to zip them together. ValueError: need more than 0 values to unpack

Carl Sandburg
  • 46
  • 1
  • 6

1 Answers1

0

I suggest that you build the structure myTable that you intend to write back to excel. It should be a list of lists

myTable = []
myRow = []

…build each myRow from your code… if the length of the list for myRow is too short, pad with proper number of [None] elements in your case if len(myRow) is 0 you need to append two “None” items

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

add the row to the output table

myTable.append(myRow)

so when ready you have a well formed nn x n table to output via:

Cell(nn, n).table = myTable
thor
  • 21,418
  • 31
  • 87
  • 173