从类

时间:2018-04-27 19:28:41

标签: python dictionary

让我们弄清楚为什么这个特殊的设置不起作用。

class market(object):
    def __init__(self, market, coin):
        self.coin = coin
        self.market = market
        req = requests.get(f"http://bittrex.com/api/v1.1/public/getticker?market={market}-{coin}")
        sum = requests.get(f"https://bittrex.com/api/v1.1/public/getmarketsummary?market={market}-{coin}")
        self.address = req.json()
        self.marketsum = sum.json()

    def ticker(self):
        while True:
            print(self.address["result"])
            time.sleep(5)

    def marketsummary(self):
        print(f"Market Summary for {coin}")
        print('_' * 20)
        print("Market Name: ", self.marketsum['result']['MarketName'])
        print("High: ", self.marketsum['result']['High']))
        print("Low: ", self.marketsum['result']['Low'])
        print("Volume: ", self.marketsum['result']['Volume'])
        print("Last: ", self.marketsum['result']['Last'])
        print("BaseVolume: ", self.marketsum['result']['BaseVolume'])
        print("TimeStamp: ", self.marketsum['result']['TimeStamp'])
        print("Bid: ", self.marketsum['result']['Bid'])
        print("Ask: ", self.marketsum['result']['Ask'])
        print("OpenBuyOrders: ", self.marketsum['result']['OpenBuyOrders'])
        print("OpenSellOrders: ", self.marketsum['result']['OpenSellOrders'])
        print("Previous Day: ", self.marketsum['result']['PrevDay'])
        print("Created: ", self.marketsum['result']['Created'])
        print("DisplayMarketName: ", self.marketsum['result']['DisplayMarketName'])`

我以前在if语句中使用了这个方法和静态(?jaron?)变量,例如

usdt_ticker = requests.get("https://bittrex.com/api/v1.1/public/getticker?market=USDT-ADA")
btc_ticker = requests.get("https://bittrex.com/api/v1.1/public/getticker?market=BTC-ADA")
eth_ticker = requests.get("https://bittrex.com/api/v1.1/public/getticker?market=ETH-ADA")

print("Which trade pairing would you like for this coin?")
tradepair = input("> ")
if str.lower(tradepair) == "usdt" or "tether":
    actual_ticker = usdt_ticker.json()
elif str.lower(tradepair) == "btc" or "bitcoin":
    actual_ticker = btc_ticker.json()
elif str.lower(tradepair) == "eth" or "ethereum":
    actual_ticker = eth_ticker.json()
else:
    print("Sorry that trading pair isnt currently being monitored by this system")
print("Now viewing Cardano /", str.upper(tradepair), " trading." )
current_price = actual_ticker["result"]["Last"]

self.marketsum['result']['MarketName']无效。如果有任何关于为什么会发生这种情况的输入以及如何解决它,我将非常感激。我得到的错误是

TypeError: list indicies must be integers or slices, not str

1 个答案:

答案 0 :(得分:1)

来自developer's guide,来自/public/getmarketsummary的回复的json结构如下所示:

{
    "success" : true,
    "message" : "",
    "result" : [{
            "MarketName" : "BTC-LTC",
            "High" : 0.01350000,
            "Low" : 0.01200000,
            "Volume" : 3833.97619253,
            "Last" : 0.01349998,
            "BaseVolume" : 47.03987026,
            "TimeStamp" : "2014-07-09T07:22:16.72",
            "Bid" : 0.01271001,
            "Ask" : 0.01291100,
            "OpenBuyOrders" : 45,
            "OpenSellOrders" : 45,
            "PrevDay" : 0.01229501,
            "Created" : "2014-02-13T00:00:00",
            "DisplayMarketName" : null
        }
    ]
}

请注意,结果实际上是一个包含单个元素的列表。他们没有说明为什么它在列表中,我不能让它返回一个包含多个元素的列表。

目前,从

更改访问marketsum的行应该没问题
self.marketsum['result']['last']

self.marketsum['result'][0]['last']

可能还会添加一个列表不为空的检查。