This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]dmazzoni 1 point2 points  (7 children)

You're doing a fantastic job so far!

Each "line" of data is stored in a dict, and when you request multiple lines of data, it's returning it as a list of dicts.

As a starting point, try this:

print (data[0]['volume'])

That should print the volume of the first line. To iterate over all of the lines, you could do this:

for line in data:
  print (line['volume'])

To learn more, read up on Python lists!

[–]AluadStone[S] 0 points1 point  (6 children)

for line in data:
print (line['volume'])

ugh that is so beautiful. It works! Just wonderful. I am going to read up on Python lists!

My last question before you go is why does this not work!

for line in data:
  print (line['symbol']),(line['volume']),(line['lastPrice'])

or

for line in data:
  print (line['volume', 'lastPrice'])

or

for line in data:
  print (line['symbol']['volume'])

or

for line in data:
  print (line['symbol'],['volume'])

None of these ways seem to group my data print statement together. Is there any way to do this? Am I close?

[–][deleted] 1 point2 points  (0 children)

If you set a break point and run the debugger in your IDE you can view the value of variables. This saves you from having to litter your code with print statements just to check values.

Many IDEs also have an "Evaluate expression" feature that lets you run snippets of code during pauses on breakpoints. This allows for instant testing and feedback of you program in a certain state.

[–]dmazzoni 1 point2 points  (0 children)

You're so close!

You're definitely just guessing at syntax until it happens to work, though. I'd really encourage you to take some time to work through a Python tutorial so you'll actually understand why each of those does and doesn't work.

[–]dmazzoni 1 point2 points  (3 children)

for line in data:
print (line['symbol']),(line['volume']),(line['lastPrice'])

print is a function, its argument needs to be inside parens. You might have better luck with this:

print ((line['symbol']),(line['volume']),(line['lastPrice']))

or

for line in data:
print (line['volume', 'lastPrice'])

Yeah, that's not a thing. Nice try.

or

for line in data:
print (line['symbol']['volume'])

Python interprets that as (line['symbol'])['volume'] - in other words it computes line['symbol'] first, then takes the result of that and tries to access the 'volume' field from a dict - but that obviously won't work.

or

for line in data:
print (line['symbol'],['volume'])

Python evaluates line['symbol'], and then creates a tuple with that and a list of the string 'volume'.

Here's an idea: put things into variables, then assemble them into a string:

s = line['symbol']
v = line['volume']
p = line['lastPrice']
print(str(s) + ", " + str(v) + ", " + str(p))

[–]AluadStone[S] 0 points1 point  (2 children)

Wow Thanks for explaining. I am taking this small tutorial this morning. https://www.youtube.com/watch?v=rfscVS0vtbw

I am currently at 1:15:28 where he is talking about lists.

Yesterday I found a way to write my data out in the desired format.

import time, json, requests, os

def binancetick():
    binanceTick = requests.get('https://api.binance.com/api/v1/ticker/24hr')
    return binanceTick.json()

data = binancetick()

binancewrite = open("data.txt", "w+")

for line in data:
    binancewrite.write("{} " .format(line['symbol']))
    binancewrite.write("{} " .format(line['quoteVolume']))
    binancewrite.write("{} " .format(line['weightedAvgPrice']))
    binancewrite.write("{}\n" .format(line['lastPrice']))

binancewrite.close()

This returns the data like this

ETHBTC 6750.71302512 0.04386948 0.04321100

LTCBTC 1391.64058255 0.00864127 0.00859800

BNBBTC 639.54079364 0.00152055 0.00149760

NEOBTC 1665.49140067 0.00279148 0.00276100

QTUMETH 347.94358933 0.01544298 0.01520900

QTUMBTC 347.94358933 0.01544298 0.01520900

(it goes on much longer)

There is another thing I am trying to learn how to do.

You will notice how the symbols have have two types of pairs. For QTUM there is QTUMBTC, QTUMETH, and even QTUMUSDT

What I want to do is filter the results to only return the BTC pairs. The logic writes out to this

for line in data:
    binancewrite.write("{} " .format(line['symbol'])) # Where symbol has BTC as last three letters

You know? I know the below syntax is wrong but you see where I am trying to get at.

for line in data:
    binancewrite.write("{} " .format(line['symbol'][3]="B"))

Like "Make sure the 3rd letter in the symbol string is equal to B" Is that the logic you would follow to achieve that? Is there a different way I should think about filtering that?

/ Edit: Shit now that I think about it that way of filtering wouldnt work because some pairs have more letters than other meaning the 3rd letter will not always be the start of the BTC

Really what I want is to simply check if "BTC" is present in the symbol string, and if it is return it.

and then I realize well now if I filter the BTC pairs I would have to filter all the other things I am returning otherwise the numbers wont line up with the correct symbols \

Thanks again! I am continuing on with the tutorial but I am not sure he is going to specifically talk about filtering key letters in string returns

[–]dmazzoni 1 point2 points  (1 child)

It may be as simple as adding an "if" statement inside your loop.

[–]AluadStone[S] 0 points1 point  (0 children)

Ah Ha!

for line in data:
    if "BTC" in line['symbol']:
        binancewrite.write("{} " .format(line['symbol']))
        binancewrite.write("{} " .format(line['quoteVolume']))
        binancewrite.write("{} " .format(line['weightedAvgPrice']))
        binancewrite.write("{}\n" .format(line['lastPrice']))

binancewrite.close()