0

I have losts of Txt files all look similar to this:

{
  "securitiesEndOfDayTrading": {
    "result": [
      {
        "ISIN": "CA0295456365",
        "basePrice": 118.2,
        "capitalListedforTrading": 760000,
        "change": 1.33,
        "changeNis": 1.23,
        "closingPrice": 423,
        "high": 129.9,
        "lastTrade": "2020-05-04T15:31:18.726Z",
        "low": 117.5,
        "marketCap": 1159100,
        "minimumAmountForContinuousTradingPhase": 21000,
        "openingPrice": 118,
        "securityID": 10112,
        "symbol": "CA1420",
        "transactionsNumber": 21,
        "turnover": 424332,
        "volume": 494332
      }
    ],
    "total": 40
  }
}

I need to create a function which knows how to read this file and store values into variables, Example: create var named basePrice and assign value 118.2 Im new to python so not sure how it can be done, i wrote this:

data1 = "basePrice"

with open("Example.txt", "r") as f:  # Read file.
    for line in f:  # Loop through every line.
        line_split = line.split()  # Split line by whitespace.
        if data1 in line_split:
            #What now???

3
  • 2
    use json module to extract the dictionary Commented Oct 7, 2020 at 6:45
  • 2
    Does this answer your question? How to parse JSON in Python? Commented Oct 7, 2020 at 6:46
  • Yes :) i was unaware of that option Commented Oct 7, 2020 at 6:50

1 Answer 1

1

When you have python or json like data stored in file use json module to extract it

import json

with open("text_file.txt", 'r') as f:
    data = json.load(f)

base_price = data['securitiesEndOfDayTrading']['result'][0]['basePrice']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.