This question seems like a silly question because its very basic, but I really am struggling. I have a constantly updating stream of data coming from python-binance web socket that is the prices of current currencies within the market. I am trying to separate the symbols and prices that comes when using the line:
info = client.get_all_tickers()
which prints the following which will be a section of the output:
[{'symbol': 'ETHBTC', 'price': '0.06327900'}, {'symbol': 'LTCBTC', 'price': '0.00406800'}, ...]
My aim is to isolate the symbol and price and have them printed next to each other instead of having the phrases 'symbol' and 'price' so I can both complete math equations on the prices, and so I can also output the two values in a beautified way.
I've tried this so far:
symbolGetter = [ swap['symbol'] for swap in info ]
to get my symbol and:
priceGetter = [ swap['price'] for swap in info ]
This only gets them separately, but trying to print them next to eachother using these methods that I thought would work:
symbolAndPriceGetter = [ swap['symbol', 'price'] for swap in info ]
print(symbolAndPriceGetter)
This throws me the error:
KeyError: ('symbol', 'price')
Trying another way:
symbolGetter = [ swap['symbol', 'price'] for swap in info ]
priceGetter = [ swap['price'] for swap in info ]
print(symbolGetter, priceGetter)
returns me the symbols printed in one list and then prices printed in another list.
How could I go about returning (symbol, price)(symbol, price)...
Thankyou for any help
You can format each item into a string like this:
stringified = ['{0} {1}'.format(swap["symbol"], swap["price"]) for swap in info]
And then you can print each of them:
for s in stringified:
print(s)
Or in a single operation:
print('\n'.join(stringified))
Both will result in:
ETHBTC 0.06327900
LTCBTC 0.00406800
On the other hand, if you wanted to write it to a csv, you can use csv.DictWriter
for that purpose:
with open('swaps.csv', 'w', newline='') as csvfile:
fieldnames = ['symbol', 'price']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(info)
Try this:
for a in info:
print(list(a.values()))
You will get:
['ETHBTC', '0.06327900']
['LTCBTC', '0.00406800']
An alternative solution would be this:
for a in info:
print(*a.values())
You will get:
ETHBTC 0.06327900
LTCBTC 0.00406800
from decimal import Decimal
dl = [{'symbol': 'ETHBTC', 'price': '0.06327900'}, {'symbol': 'LTCBTC', 'price': '0.00406800'}]
res = [(d['symbol'], Decimal(d['price'])) for d in dl]
print(res)
# [('ETHBTC', Decimal('0.06327900')), ('LTCBTC', Decimal('0.00406800'))]
# Then for example if you want aveage of price
prices = [t[1] for t in res]
ave_price = sum(prices) / (len(prices))
print(ave_price)
# 0.03367350
You can put expressions in the list comprehension, eg:
prices_as_tuples = [ (swap['symbol'], swap['price']) for swap in info ]
Or, to format it up as text
prices_for_printing = [ "%s at %s" % (swap['symbol'], swap['price']) for swap in info ]
print("; ".join(prices_for_printing))
You can iterate over the values of each dictionary and print them using generator
print(*(' '.join(v for v in d.values()) for d in info), sep='\n')
# ETHBTC 0.06327900
# LTCBTC 0.00406800
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.