- Python: Opening tab delimited csv file and loading the data to a list of dicts
- i = 0
- rows = []
- with open('data/AAPLUSUSD_H4.csv', 'r', encoding='utf-8') as f:
- for row in f:
- print(row.strip().split('\t'))
- rows.append(row.strip().split('\t'))
- i += 1
- if i > 3:
- break
- rows_dict = [dict(zip(rows[0], row)) for row in rows[1:]]
- print(rows_dict)
- >>>['Time', 'Open', 'High', 'Low', 'Close', 'Volume']
- ['2017-02-01 20:00:00', '127.07', '128.43', '127.06', '128.163', '90']
- ['2017-02-02 00:00:00', '128.16', '130.112', '127.851', '129.983', '240']
- ['2017-02-02 04:00:00', '129.984', '130.48', '128.78', '128.785', '60']
- [{'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'}]
- 1) Saving list of dicts to JSON file using json.dump()
- import json
- with open('data/test.json', 'w', encoding='utf-8') as f:
- json.dump(rows_dict, f, ensure_ascii=False, indent=4)
- 2) Saving list of dicts to json file using pandas dataframe and to_json()
- import pandas as pd
- df = pd.DataFrame(data=rows[1:], columns=rows[0])
- json_rows = df.to_json('data/test_pandas.json', orient='records')
- ### Note: for the df.to_json method, the orient='records' needs to be used to get the output similar to the one listed in #1.
- There are different ways to format the JSON string. You’ll need to set the orient to your desired format. Here are the options:
- split
- records
- index
- values
- table
- columns (the default format)
- For more info: https://datatofish.com/export-pandas-dataframe-json/
- The json output file will look like this: test.json and test_pandas.json will both contain this same data.
- [
- {
- "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"
- }
- ]
- How To Use JSON In Python
- https://www.youtube.com/watch?v=-51jxlQaxyA
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