Skip to content Skip to sidebar Skip to footer

How To Create A Dictionary Of The Contents Of An Excel Spreadsheet?

I'm attempting to build a dictionary in Python based off the contents of an Excel spreadsheet file. Here is an example of how the spreadsheet is structured (two columns): Col 1

Solution 1:

[{'Hello': 'World', 'Earth', 'Planet', 'Mars', 'Moon'), ('Hi': 'Pluto', 'Neptune', 'Jupiter'}] is not valid python dictionary. You can't have multiple values for a key.

However, you can have the value as a list of items, like this

{
    'Hello': [
        'World',
        'Earth',
        'Planet',
        'Mars',
        'Moon'
    ],
    'Hi': [
        'Pluto',
        'Neptune',
        'Jupiter'
    ]
}

Solution 2:

With Pandas you can do this with:

import pandas as pddf= pd.read_excel('data1.xlsx')
dictionary = df.to_dict()

Post a Comment for "How To Create A Dictionary Of The Contents Of An Excel Spreadsheet?"