Create a Python dictionary from Terminal

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:

  1. On your MacBook, open Terminal.
  2. In Terminal, run the following script:

Figure 1: Script for Python and openpyxl.

  1. 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.


Spreadsheet and output:

Figure 3: Excel spreadsheet.

Figure 4: Python output.