0

i'd like to import certain columns in this csv into a nested python dict:

Name, Type, ID, Job, Height
Adam, Man, asmith, factory, 5
Ben, Man, bjones, mine, 6
Jamie, Woman, jbarnes, bank, 5.5

output:

dict1 = { asmith: {Name:Adam, Type:Man, Height:5},
          bjones, {Name:Ben, Type:Man, Height:6},
          jbarnes:, {Name:Jamie,Type:Woman, Height:5.5} }
1
  • forgot to mention: the headers are on the 2nd row & is there a specific way to select columns to use (sometimes I want to exclude columns - this ex i'd like to exclude job) Commented Jul 24, 2017 at 19:41

1 Answer 1

1

We can use the DictReader from csv for this:

from csv import DictReader

with open('data.csv') as csvfile:
    reader = DictReader(csvfile)
    result = {row[' ID'] : row for row in reader}

Now result will be a dictionary that maps IDs to dictionaries. The dictionary will contain the 'ID' as well. Now result will be:

{' bjones': {'Name': 'Ben', ' Type': ' Man', ' Height': ' 6', ' ID': ' bjones', ' Job': ' mine'}, ' jbarnes': {'Name': 'Jamie', ' Type': ' Woman', ' Height': ' 5.5', ' ID': ' jbarnes', ' Job': ' bank'}, ' asmith': {'Name': 'Adam', ' Type': ' Man', ' Height': ' 5', ' ID': ' asmith', ' Job': ' factory'}}

As we can see the values are not stripped: these contain spaces on the left and the right. We can process these as follows:

from csv import DictReader

with open('data.csv') as csvfile:
    reader = DictReader(csvfile)
    result = {}
    for row in reader:
        row = {k.strip():v.strip() for k,v in row.items()}
        result[row.pop('ID')] = row

This will remove the ID key from the dictionaries as well. Now the answer is:

>>> result
{'jbarnes': {'Name': 'Jamie', 'Height': '5.5', 'Job': 'bank', 'Type': 'Woman'}, 'bjones': {'Name': 'Ben', 'Height': '6', 'Job': 'mine', 'Type': 'Man'}, 'asmith': {'Name': 'Adam', 'Height': '5', 'Job': 'factory', 'Type': 'Man'}}

EDIT: In case you want to ignore the first line, you can call next(..) on the file handler first:

from csv import DictReader

with open('data.csv') as csvfile:
    next(csvfile)
    reader = DictReader(csvfile)
    result = {}
    for row in reader:
        row = {k.strip():v.strip() for k,v in row.items()}
        result[row.pop('ID')] = row
Sign up to request clarification or add additional context in comments.

1 Comment

I would suggest using row.pop('ID') to remove it from the dictionary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.