You can convert an Excel spreadsheet to a formatted Python dictionary and export it as a text file using Terminal on your MacBook. The process takes column headers on the spreadsheet and makes them dictionary keys. If you use a nested dictionary, the column headers should match the categories, but the first column header is not used when creating the dictionary.
Note: Prior to completing the steps below, make sure you have Python and openpyxl installed.
To create a Python dictionary from Terminal:
- On your MacBook, open Terminal.
- In Terminal, run the following script:
Figure 1: Script for Python and openpyxl.
- With the exception of the lines beginning with #, which are comments, enter the following script:
Important: Make sure you follow the structure of the script. If your indentation is off, you will get a syntax error and have to start over.
Figure 2: Create a Python dictionary.
Example:
Spreadsheet and output:
Figure 3: Excel spreadsheet.
Figure 4: Python output.