Reputation: 501
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???
Upvotes: 0
Views: 278
Reputation: 975
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']
Upvotes: 1