2

This question already has an answer here:

Using the Krakenex Python module, I am pulling in prices from the Kraken API and it arrives in the following format:

{u'result': {u'XXBTZEUR': {u'a': [u'214.79000', u'3'], u'c': [u'214.79416', u'2.27789000'], u'b': [u'214.15262', u'7'], u'h': [u'217.36000', u'217.36000'], u'l': [u'210.99999', u'210.99999'], u'o': u'214.01000', u'p': [u'213.77705', u'213.51830'], u't': [1360, 1499], u'v': [u'872.87753147', u'1036.51819483']}}, u'error': []}

(this is the output of 'ticker' in the code below)

With dictionary manipulation I can get the last closing price, in this case 214.79416:

last_close_raw = ticker["result"]["XXBTZEUR"]["c"]
last_close = last_close_raw[0]

This seems to work until I feed the closing prices into a list, at which point the u' reappears. This is the full code:

from time import strftime
import krakenex

k = krakenex.API()

x = []
y = []

count = 0

while count <= 9:

    ticker = k.query_public('Ticker', {'pair': 'XXBTZEUR'})

    last_close_raw = ticker["result"]["XXBTZEUR"]["c"]
    last_close = last_close_raw[0]

    timenow = strftime("%H:%M:%S")

    print "%s ----> %s\n----------(%s)" % (timenow, last_close, count)

    x.append(count)
    y.append(last_close)

    count += 1

print "x = ", x
print "y = ", y

This is the output:

23:07:03 ----> 214.79416
----------(0)
23:07:05 ----> 214.79416
----------(1)
23:07:06 ----> 214.79416
----------(2)
23:07:07 ----> 214.79416
----------(3)
23:07:07 ----> 214.79416
----------(4)
23:07:08 ----> 214.79416
----------(5)
23:07:09 ----> 214.79416
----------(6)
23:07:10 ----> 214.79416
----------(7)
23:07:11 ----> 214.79416
----------(8)
23:07:12 ----> 214.79416
----------(9)
x =  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y =  [u'214.79416', u'214.79416', u'214.79416', u'214.79416', u'214.79416', u'214.79416', u'214.79416', u'214.79416', u'214.79416', u'214.79416']

Why does a list bring back the u'? I have even tried removing the first two characters from each price but this removes the first two digits, not the u'. Thoughts?

1 답변


2

What bothers you is the difference between Unicode string and ASCII string. To remove the u use encode :

>>> a=u'214.79416'
>>> type(a)
<type 'unicode'>
>>> b = a.encode('ascii','ignore')
>>> type(b)
<type 'str'>
>>> b
'214.79416'

Hope this will hepl.


  • Thanks! This worked, as did str(unicode(last_close) which I found in the link posted by alfasin - Jack Sellers

Linked


Related

Latest