0

I know there are many other questions out there with the answer to this, but I want to know how to read a CSV file into a nested dictionary. So it would go from something like this:

    '''"YEAR","GENDER","NAME","COUNT"
    "2011","FEMALE","A","100"
    "2012","FEMALE","A","50"
    "2012","FEMALE","B","10"
    "2012","FEMALE","C","10"
    "2012","FEMALE","D","5"
    "2013","FEMALE","A","1000"
    '''

To something like this:

{('B', 'FEMALE'): {2012: (10, None)}, 
 ('C', 'FEMALE'): {2012: (10, None)}, 
 ('A', 'FEMALE'): {2011: (100, None), 
                   2012: (50, None), 
                   2013: (1000, None)}, 
 ('D', 'FEMALE'): {2012: (5, None)}}

But I need to do it without importing anything, so I can't use the CSV module. And this is a general example; I'd need the code to work for multiple cases that could be longer or shorter than this one.

(There are 11 files total, this being one of them, and the text here is exactly as it is in the file:

    "YEAR","GENDER","NAME","COUNT"
    "2011","FEMALE","A","100"
    "2012","FEMALE","A","50"
    "2012","FEMALE","B","10"
    "2012","FEMALE","C","10"
    "2012","FEMALE","D","5"
    "2013","FEMALE","A","1000"
1
  • 2
    if you don't mind me asking, why can't you import anything? Commented Nov 8, 2015 at 0:48

1 Answer 1

1
answer = {}
with open('path/to/file') as infile:
    infile.readline()  # we don't care about the header row
    for line in infile:
        year, gender, name, count = (s.strip('"') for s in line.split(','))
        key = (name, gender)
        if key not in answer: answer[key] = {}
        answer[key][int(year)] = (int(count), None)
Sign up to request clarification or add additional context in comments.

8 Comments

Sorry, new to this and didn't notice the small add a comment button. I downvoted because the code just gave me value errors when I tested it.
@jtdurb96: if you could show me your error, I'd be happy to update my answer
Well, one value error said "ValueError: too many values to unpack (expected 4)" and the other seven say "ValueError: invalid literal for int() with base 10" and then the number for count in quotes.
The code I gave you works on the document you posted. Please edit your original post to include a representative sample of the input
So I tried bringing back up the errors I had before on the command prompt, but now the error is the same for all of them, saying "ValueError: I/O operation on closed file." My code is exactly as you typed it, except for it all being used to define a function.
|

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.