The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Python Help: Open JSON file and get timestamp of highest Open value from dataset. AAPL stock sample data.
By Guest on 12th November 2022 01:22:56 AM | Syntax: PYTHON | Views: 282



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. #read the json data back
  2. import json
  3.  
  4. with open('data/test.json', 'r') as f:
  5.     reader = json.load(f)
  6.     #print(reader)
  7.  
  8. openvalues = {}
  9.  
  10. #find the time of the highest Open value
  11. for item in reader:
  12.     #print(item) #{'Time': '2017-02-02 04:00:00', 'Open': '129.984', 'High': '130.48', 'Low': '128.78', 'Close': '128.785', 'Volume': '60'}
  13.     openvalues[float(item['Open'])] = item['Time']
  14.  
  15. print('Open values available with timestamps')
  16. print(openvalues)
  17.  
  18. openkeys = list(openvalues.keys())
  19.  
  20. print('Open values available as list of keys')
  21. print(openkeys)
  22.  
  23. print('Sort the keys desc order')
  24. openkeys.sort(reverse=True)
  25.  
  26. print(openkeys)
  27.  
  28. print('highest open value:')
  29. print(openkeys[0])
  30. #the list is in descending order so the highest value will be the first element
  31. print('timestamp of highest open value:')
  32. print(openvalues[openkeys[0]])
  33.  
  34.  
  35. >>>Open values available with timestamps
  36. {127.07: '2017-02-01 20:00:00', 128.16: '2017-02-02 00:00:00', 129.984: '2017-02-02 04:00:00'}
  37. Open values available as list of keys
  38. [127.07, 128.16, 129.984]
  39. Sort the keys desc order
  40. [129.984, 128.16, 127.07]
  41. highest open value:
  42. 129.984
  43. timestamp of highest open value:
  44. 2017-02-02 04:00:00
  45.  
  46.  
  47. ############################################
  48. The json data file looks like this:
  49.  
  50. [
  51.     {
  52.         "Time": "2017-02-01 20:00:00",
  53.         "Open": "127.07",
  54.         "High": "128.43",
  55.         "Low": "127.06",
  56.         "Close": "128.163",
  57.         "Volume": "90"
  58.     },
  59.     {
  60.         "Time": "2017-02-02 00:00:00",
  61.         "Open": "128.16",
  62.         "High": "130.112",
  63.         "Low": "127.851",
  64.         "Close": "129.983",
  65.         "Volume": "240"
  66.     },
  67.     {
  68.         "Time": "2017-02-02 04:00:00",
  69.         "Open": "129.984",
  70.         "High": "130.48",
  71.         "Low": "128.78",
  72.         "Close": "128.785",
  73.         "Volume": "60"
  74.     }
  75. ]
  76.  
  77.  
  78. Sorting in Python || Learn Python Programming
  79. 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
  • Recent Pastes