how to use dictionary - newby

Joined
Apr 2, 2013
Messages
4
Reaction score
0
using python 2.5. need help with dictionary. i know how to create it, but i can't seem to understand how to use it in a practical way. I have a set of data extracted from a database - example, States and Cities. while the state is in one field, the cities are in another field separated by comas with associated numbers (relative size)

my code so far:

import win32api, win32com.client
import os, sys, string
import csv

try:
f = open(file,'r')
reader = csv.reader(f)
# save header items
headerList = reader.next()
dataDict = {}
for line in reader:
dataDict.setdefault(line[0], []).append(line[-2]) #State
dataDict.setdefault(line[0], []).append(line[-1]) #city
f.close()
except Exception, e:
f.close
amsgui.ViewPrompt([e])

in the dictionary i have :
{'CA': ['Sacramento6,San Francisco4,San Diego2,Fresno5,Los Angeles1,San Jose3],'WA':['Seatle1,Tacoma3,Yakima8,Spokane2]}

and so forth
what i need to do (but can't quite grasp from reading the examples) is how to cycle thru each key(state), capture the associated city and plug that into a spreadsheet so that if would look like:
Col A Col B Col C
CA Los Angeles 1
CA San Diego 2
CA San Jose 3
....
WA Seatle 1
WA Spokane 2

etc

appologies for the newby question
i need a point in the right direction. is this even the correct approach?

thanx,
gman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top