使用Krakenex Python模块,我从Kraken API中提取价格,它的格式如下:
{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': []}
(这是下面代码中'ticker'的输出)
通过字典操作,我可以获得最后的收盘价,在本例中为214.79416:
last_close_raw = ticker["result"]["XXBTZEUR"]["c"]
last_close = last_close_raw[0]
这似乎有效,直到我将收盘价格提供给一个列表,此时你会再次出现。这是完整的代码:
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
这是输出:
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']
为什么列表会带回你的?我甚至尝试从每个价格中删除前两个字符,但这会删除前两个数字,而不是u'。想法?
答案 0 :(得分:2)
困扰你的是Unicode字符串和ASCII字符串之间的区别。要删除u
使用encode
:
>>> a=u'214.79416'
>>> type(a)
<type 'unicode'>
>>> b = a.encode('ascii','ignore')
>>> type(b)
<type 'str'>
>>> b
'214.79416'
希望这会是肝脏。