-4

If I have a CSV file that looks like this:

Name | Value 1 | Value 2

Foobar | 22558841 | 96655
Barfool | 02233144 | 3301144

How can I make it into a dictionary that looks like this:

dict = {
    'Foobar': {
        'Value 1': 2255841,
        'Value 2': 9665
    },
'Barfool': {
        'Value 1': 02233144,
        'Value 2': 3301144
    }
}

1 Answers1

1

If you use pandas:

import pandas as pd
pd.read_csv('test.csv', delimiter='|', index_col='Name').T.to_dict()

# {'Barfool': {'Value 1': 2233144, 'Value 2': 3301144},
#  'Foobar': {'Value 1': 22558841, 'Value 2': 96655}}
Julien Spronck
  • 15,069
  • 4
  • 47
  • 55