Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Dict Values from csv.DictReader

Tags:

dictionary

csv

I'm trying to take a csv file and turn it into a dictionary, via csv.DictReader. After doing this, I want to modify one of the columns of the dictionary, and then write the data into a tsv file. I'm dealing with words and word frequencies in a text.

I've tried using the dict.value() function to obtain the dictionary values, but I get an error message saying "AttributeError: DictReader instance has no attribute "values""

Below is my code:

#calculate frequencies of each word in Jane Austen's "Pride and Prejudice"
import csv

#open file with words and counts for the book, and turn into dictionary
fob = open("P&P.csv", "r")
words = csv.DictReader(fob)
dict = words

#open a file to write the words and frequencies to
fob = open("AustenWords.tsv", "w")

#set total word count
wordcount = 120697

for row in words:
    values = dict.values()
    print values

Basically, I have the total count of each word in the text (i.e. "a","1937") and I want to find the percentage of the total word count that the word in question uses (thus, for "a", the percentage would be 1937/120697.) Right now my code doesn't have the equation for doing this, but I'm hoping, once I obtain the values of each row, to write a row to the new file with the word and the calculated percentage. If anyone has a better way (or any way!) to do this, I would greatly appreciate any input.

Thanks

like image 414
Sophie Harrington Avatar asked Feb 21 '11 01:02

Sophie Harrington


People also ask

What does csv DictReader return?

The csv. DictReader() returned an OrderedDict type for each row. That's why we used dict() to convert each row to a dictionary. Notice that we have explicitly used the dict() method to create dictionaries inside the for loop.

What is the use of csv DictReader () class?

CSV, or "comma-separated values", is a common file format for data. The csv module helps you to elegantly process data stored within a CSV file.

How do I read a CSV file in a dictionary?

The best way to convert a CSV file to a Python dictionary is to create a CSV file object f using open("my_file. csv") and pass it in the csv. DictReader(f) method. The return value is an iterable of dictionaries, one per row in the CSV file, that maps the column header from the first row to the specific row value.


2 Answers

To answer the basic question - "why am I getting this error" - when you call csv.DictReader(), the return type is an iterator not a Dictionary.

Each ROW in the iterator is a Dictionary which you can then use for your script:

for row in words:    
    values = row.values()    
    print values
like image 200
matthewdunnam Avatar answered Sep 23 '22 05:09

matthewdunnam


Thank goodness for Matt Dunnam's answer (I'd reply to it but I don't see how to). csv.DictReader objects are, quite counter-intuitively, NOT dictionary objects (although I think I am beginning to see some usefulness in why not). As he says, csv.DictReader objects are an iterator (with my intro level to python, I think this is like a list maybe). Each entry in that object (which is not a dictionary) is a dictionary.

So, csv.DictReader returns something like a list of dictionaries, which is not the same as returning one dictionary object, despite the name.

What is nice, so far, is that csv.DictReader did preserve my key values in the first row, and placed them correctly in each of the many dictionary objects that are a part of the iterable object it actually returned (again, it does not return a dictionary object!).

I've wasted about an hour banging my head on this, the documentation is not clear enough, although now that I understand what type of object csv.DictReader returns, the documentation is a lot clearer. I think the documentation says something like how it returns an iterable object, but if you think it returns a dictionary and you don't know if dictionaries are iterable or not then this is easy to read as "returns a dictionary object".

The documentation should say "This does not return a dictionary object, but instead returns an iterable object containing a dictionary object for each entry" or some such thing. As a python newbie who hasn't coded in 20 years, I keep running into problems where the documentation is written by and for experts and it is too dense for beginners.

I'm glad it's there and that people have given their time to it, but it could be made easier for beginners while not reducing its worth to expert pythonistas.

like image 37
Nat Poor Avatar answered Sep 21 '22 05:09

Nat Poor