Search code examples
pythonlistbinance

How to extract values from strings


Hi I am working on a trading bot and i have a question.How do i extract symbol and price value from this type of string. I want to store them in different variables like price and symbol.

[
    {
        "symbol": "LTCBTC",
        "price": "4.00000200"
    },
    {
        "symbol": "ETHBTC",
        "price": "0.07946600"
    }
]

Solution

  • import json
    
    binance_str = '[{"symbol": "LTCBTC", "price": "4.00000200" }, { "symbol": "ETHBTC", "price": "0.07946600"}]'
    binance_json = json.loads(binance_str)
    
    for el in binance_json:
        print(el['symbol'])
        print(el['price'])
    
    # Output:
    # =======
    # LTCBTC
    # 4.00000200
    # ETHBTC
    # 0.07946600