[NEWB] Dictionary instantiation?
Shane Geiger
sgeiger at ncee.net
Fri Dec 7 16:34:58 EST 2007
#!/usr/bin/python
"""
EXAMPLE USAGE OF PYTHON'S CSV.DICTREADER FOR PEOPLE NEW TO PYTHON
Python - Batteries Included(tm)
This file will demonstrate that when you use the python CSV module, you
don't have to remove the newline characters, as between "acorp_ Ac" and
"orp Foundation" and other parts of the data below.
It also demonstrates python's csv.DictReader, which allows you to read a
CSV record into a dictionary.
This will also demonstrate the use of lists ([]s) and dicts ({}s).
If this doesn't whet your appetite for getting ahold of a powertool
instead of sed for managing CSV data, I don't know what will.
"""
#### FIRST: CREATE A TEMPORARY CSV FILE FOR DEMONSTRATION PURPOSES
mycsvdata = """
"Category","0","acorp_ Ac
orp Foundation","","","Acorp Co","(480) 905-1906","877-462-5267 toll
free","800-367-2228","800-367-2228","info at acorp.or
g","7895 East Drive","Scottsdale","AZ","85260-6916","","","","","","Pres
Fred & Linda ","0","0","1","3","4","1"
"Category","0","acorp_ Bob and Margaret Schwartz","","","","317-321-6030
her","317-352-0844","","","","321 North Butler Ave.","In
dianapolis","IN","46219","","","","","","Refrigeration
man","0","1","2","3","4","0"
"Category","0","acorp_ Elschlager,
Bob","","","","","702-248-4556","","","TropBob at aol.com","7950 W.
Flamingo Rd. #2032","Las Vega
s","NV","89117","","","","","","guy I met","0","1","2","3","4","1"
"""
## NOTE: IF YOU HAVE A RECORD SEPARATOR WITHIN QUOTES, IT WILL NOT BE
TREATED LIKE A RECORD SEPARATOR!
## Beef|"P|otatos"|Dinner Roll|Ice Cream
import os, sys
def writefile(filename, filedata, perms=750):
f = open(filename, "w")
f.write(filedata)
os.system("chmod "+str(perms)+" "+filename)
f.close()
file2write = 'mycsvdata.txt'
writefile(file2write,mycsvdata)
# Check that the file exists
if not os.path.exists(file2write):
print "ERROR: unable to write file:", file2write," Exiting now!"
sys.exit()
# ...so everything down to this point merely creates the
# temporary CSV file for the code to test (below).
#### SECOND: READ IN THE CSV FILE TO CREATE A LIST OF PYTHON
DICTIONARIES, WHERE EACH
# DICTIONARY CONTAINS THE DATA FROM ONE ROW. THE KEYS OF THE
DICTIONARY WILL BE THE FIELD NAMES
# AND THE VALUES OF THE DICTIONARY WILL BE THE VALUES CONTAINED WITHIN
THE CSV FILE'S ROW.
import csv
### NOTE: Modify this list to match the fields of the CSV file.
header_flds =
['cat','num','name','blank1','blank2','company','phone1','phone2', \
'phone3','phone4','email','addr1','city','state','zip','blank3', \
'blank4','blank5','blank6','blank7','title','misc1','misc2','misc3', \
'mics4','misc5','misc6']
file2open = 'mycsvdata.txt'
reader = csv.DictReader(open(file2open), [], delimiter=",")
data = []
while True:
try:
# Read next "header" line (if there isn't one then exit the loop)
reader.fieldnames = header_flds
rdr = reader.next()
data.append(rdr)
except StopIteration: break
def splitjoin(x):
""" This removes any nasty \n that might exist in a field
(of course, if you want that in the field, don't use this)
"""
return ''.join((x).split('\n'))
#### THIRD: ITERATE OVER THE LIST OF DICTS (IN WHICH EACH DICT IS A
ROW/RECORD FROM THE CSV FILE)
# example of accessing all the dictionaries once they are in the list
'data':
import string
for rec in data: # for each CVS record
itmz = rec.items() # get the items from the dictionary
print "- = " * 20
for key,val in itmz:
print key.upper()+": \t\t",splitjoin(val)
# Note: splitjoin() allows a record to contain fields
with newline characters
Matt_D 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!
>
--
Shane Geiger
IT Director
National Council on Economic Education
sgeiger at ncee.net | 402-438-8958 | http://www.ncee.net
Leading the Campaign for Economic and Financial Literacy
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 257 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20071207/3711acce/attachment.sig>
More information about the Python-list
mailing list