Dictionary instantiation?

Matt_D matt.deboard at gmail.com
Fri Dec 7 05:50:58 EST 2007


On Dec 7, 12:33 pm, Matt_D <matt.debo... at gmail.com> wrote:
> On Dec 7, 12:27 pm, Matt_D <matt.debo... at gmail.com> wrote:
>
>
>
> > On Dec 7, 11:42 am, Virgil Dupras <hardcoded.softw... at gmail.com>
> > wrote:
>
> > > On Dec 7, 9:05 am, Matt_D <matt.debo... at gmail.com> wrote:
>
> > > > Hello there, this is my first post to the list. Only been working with
> > > > Python for a few days. Basically a complete newbie to programming.
>
> > > > I'm working with csv module as an exercise to parse out a spreadsheet
> > > > I use for work.(I am an editor for a military journalism unit) Not
> > > > trying to do anything useful, just trying to manipulate the data.
> > > > Anyway, here's the code I've got so far:
>
> > > > import csv
> > > > import string
> > > > import os
>
> > > > #Open the appropriate .csv file
> > > > csv_file = csv.reader(open("D:\\Python25\\BNSR.csv"))
>
> > > > #Create blank dictionary to hold {[author]:[no. of stories]} data
> > > > story_per_author = {}
>
> > > > def author_to_dict(): #Function to add each author to the dictionary
> > > > once to get initial entry for that author
> > > >     for row in csv_file:
> > > >         author_count = row[-1]
> > > >         story_per_author[author_count] = 1
>
> > > > #Fetch author names
> > > > def rem_blank_authors(): #Function to remove entries with '' in the
> > > > AUTHOR field of the .csv
> > > >     csv_list = list(csv_file) #Convert the open file to list format
> > > > for e-z mode editing
> > > >     for row in csv_list:
> > > >         author_name = row[-1]
> > > >         if author_name == '': #Find entries where no author is listed
> > > >             csv_list.remove(row) #Remove those entries from the list
>
> > > > def assign_author_to_title(): #Assign an author to every title
> > > >     author_of_title = {}
> > > >     for row in csv_file:
> > > >         title = row[3]
> > > >         author = row[-1]
> > > >         author_of_title[title] = author
>
> > > > assign_author_to_title()
> > > > print author_of_title
>
> > > > --
>
> > > > Ok, the last two lines are kind of my "test the last function" test.
> > > > Now when I run these two lines I get the error:
>
> > > > Traceback (most recent call last):
> > > >   File "D:\Python25\Lib\SITE-P~1\PYTHON~1\pywin\framework
> > > > \scriptutils.py", line 310, in RunScript
> > > >     exec codeObject in __main__.__dict__
> > > >   File "D:\Python25\csv_read.py", line 33, in <module>
> > > >     print author_of_title
> > > > NameError: name 'author_of_title' is not defined
>
> > > > I am guessing that the author_of_title dict does not exist outside of
> > > > the function in which it is created? The concept of instantiation is
> > > > sort of foreign to me so I'm having some trouble predicting when it
> > > > happens.
>
> > > > If I call the assign_author_to_title function later, am I going to be
> > > > able to work with the author_of_title dictionary? Or is it best if I
> > > > create author_of_title outside of my function definitions?
>
> > > > Clearly I'm just stepping through my thought process right now,
> > > > creating functions as I see a need for them. I'm sure the code is
> > > > sloppy and terrible but please be gentle!
>
> > > As you said, author_of_title doesn't exist outside of
> > > assign_author_to_title() because it has been instantiated in the
> > > function, and thus belong to the local scope. You could instantiate
> > > your dictionary outside of the function, but the nicest way to handle
> > > this would be to add a line "return author_of_title" at the end of
> > > assign_author_to_title() and have "print assign_author_to_title()"
> > > instead of the 2 last lines.
>
> > Another newb question, same project:
>
> > #Fetch author names
> > def rem_blank_authors(): #Function to remove entries with '' in the
> > AUTHOR field of the .csv
> >     csv_list = list(csv_file) #Convert the open file to list format
> > for e-z mode editing
> >     for row in csv_list:
> >         author_name = row[-1]
> >         if author_name == '': #Find entries where no author is listed
> >             csv_list.remove(row) #Remove those entries from the list
> >         return csv_list
>
> > def author_to_dict(): #Function to add each author to the dictionary
> > once to get initial entry for that author
> >     #rem_blank_authors() #Call this function to remove blank author
> > fields before building the main dictionary
> >     for row in csv_file:
> >         author_count = row[-1]
> >         if author_count in story_per_author:
> >             story_per_author[author_count] += 1
> >         else:
> >             story_per_author[author_count] = 1
> >     return story_per_author
>
> > def assign_author_to_title(): #Assign an author to every title
> >     author_of_title = {}
> >     for row in csv_file:
> >         title = row[3]
> >         author = row[-1]
> >         author_of_title[title] = author
>
> > author_to_dict()
> > print story_per_author
>
> > --
>
> > The solution provided for my previous post worked out. Now I'm testing
> > the author_to_dict function, modified to get an accurate count of
> > stories each author has written. Now, if I call rem_blank_authors,
> > story_per_author == {}. But if I #comment out that line, it returns
> > the expected key values in story_per_author. What is happening in
> > rem_blank_authors that is returning no keys in the dictionary?
>
> > I'm afraid I don't really understand the mechanics of "return" and
> > searching the docs hasn't yielded too much help since "return" is such
> > a common word (in both the Python 2.5 docs and Dive Into Python). I
> > realize I should probably RTFM, but honestly, I have tried and can't
> > find a good answer. Can I get a down and dirty explanation of exactly
> > what "return" does? And why it's sometimes "return" and sometimes it
> > has an argument? (i.e. "return" vs. "return author_of_title")
>
> Oop, made this last post before seeing Bruno's. Still have the same
> questions but haven't implemented his suggestions.

Wow, list spam.

Sorry about this.

Anyway, disregard my last two. I get it now. When a variable is
defined inside a function, and modifications are made to it, that
variable must be passed to module. In this specific example when I:

>return csv_file

in the rem_blank_authors() function it goes *back* to author_to_dict
using the now-available csv_file variable as its parameter. The
problem in that I was forgetting that each row in the csv file is a
discrete item in the list generated by csv.reader. I think I got it
figured now. Thanks for all the help.



More information about the Python-list mailing list