- #read the json data back
- import json
- with open('data/test.json', 'r') as f:
- reader = json.load(f)
- #print(reader)
- openvalues = {}
- #find the time of the highest Open value
- for item in reader:
- #print(item) #{'Time': '2017-02-02 04:00:00', 'Open': '129.984', 'High': '130.48', 'Low': '128.78', 'Close': '128.785', 'Volume': '60'}
- openvalues[float(item['Open'])] = item['Time']
- print('Open values available with timestamps')
- print(openvalues)
- openkeys = list(openvalues.keys())
- print('Open values available as list of keys')
- print(openkeys)
- print('Sort the keys desc order')
- openkeys.sort(reverse=True)
- print(openkeys)
- print('highest open value:')
- print(openkeys[0])
- #the list is in descending order so the highest value will be the first element
- print('timestamp of highest open value:')
- print(openvalues[openkeys[0]])
- >>>Open values available with timestamps
- {127.07: '2017-02-01 20:00:00', 128.16: '2017-02-02 00:00:00', 129.984: '2017-02-02 04:00:00'}
- Open values available as list of keys
- [127.07, 128.16, 129.984]
- Sort the keys desc order
- [129.984, 128.16, 127.07]
- highest open value:
- 129.984
- timestamp of highest open value:
- 2017-02-02 04:00:00
- ############################################
- The json data file looks like this:
- [
- {
- "Time": "2017-02-01 20:00:00",
- "Open": "127.07",
- "High": "128.43",
- "Low": "127.06",
- "Close": "128.163",
- "Volume": "90"
- },
- {
- "Time": "2017-02-02 00:00:00",
- "Open": "128.16",
- "High": "130.112",
- "Low": "127.851",
- "Close": "129.983",
- "Volume": "240"
- },
- {
- "Time": "2017-02-02 04:00:00",
- "Open": "129.984",
- "High": "130.48",
- "Low": "128.78",
- "Close": "128.785",
- "Volume": "60"
- }
- ]
- Sorting in Python || Learn Python Programming
- https://www.youtube.com/watch?v=QtwhlHP_tqc
Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.
Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.
See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license