0

I need to take a .csv file, open the file, read the file, then create a database, which is a dictionary whose keys are years and values are a list with a tuple in it, like so:

db = {2002:[('Andrew', 5, 63, 75000)], etc…}

The categories that I need are 'Name', Category, deaths, DamageMillions

This is a snap of the data that I am working with:

enter image description here

How would I do this?

Edit: I know how to open and read the file, I just don't know how to create the database.

1
  • 1
    The spreadsheet is a database! What more do you want? Commented Jul 9, 2015 at 15:41

2 Answers 2

2

This is untested but should give you a good idea where to start:

import csv
from collections import defaultdict

db = defaultdict(list)
with open('csvfile.csv') as f:
     csvreader = csv.reader(f)
     for row in csvreader:
         db[row[0]].append((row[1], row[4], row[5], row[6]))

return db

More info on defaultdict and the csv module.

Sign up to request clarification or add additional context in comments.

Comments

0

Use the csv module in conjunction with DictReader. You'll get a list of dictionaries to iterate over, which you'll have to transform to get your desired year indices.

See: https://docs.python.org/2/library/csv.html

Comments

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.