有问题弄清楚了为什么这种特殊设置无法正常工作。

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语句中将此方法与static(?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

最佳答案

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']


可能还会添加一个检查,以确保列表不为空。

关于python - 从嵌套字典中打印类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50069108/

10-12 21:20