how to use dictionary - newby

Discussion in 'Python' started by GMAN1941, Jul 23, 2013.

  1. GMAN1941

    GMAN1941

    Joined:
    Apr 2, 2013
    Messages:
    4
    Likes Received:
    0
    Location:
    huntsville, al
    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
     
    GMAN1941, Jul 23, 2013
    #1
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. niko

    Gadfly use (Newby)

    niko, Aug 16, 2005, in forum: Python
    Replies:
    3
    Views:
    621
    Dennis Lee Bieber
    Aug 17, 2005
  2. Ilias Lazaridis
    Replies:
    6
    Views:
    800
    Ilias Lazaridis
    Feb 21, 2006
  3. james_027
    Replies:
    1
    Views:
    613
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  4. Navkirat Singh
    Replies:
    6
    Views:
    6,137
    Navkirat Singh
    Jul 29, 2010
  5. Zaiki
    Replies:
    0
    Views:
    252
    Zaiki
    Jun 6, 2005
  6. Fox

    dictionary within dictionary

    Fox, Mar 8, 2005, in forum: ASP General
    Replies:
    5
    Views:
    511
    Michael D. Kersey
    Mar 13, 2005
  7. Jaewoong kim Crossbreeze
    Replies:
    1
    Views:
    354
    Jaewoong kim Crossbreeze
    Apr 17, 2009
  8. Ayushi Dalmia

    Initialise dictionary of dictionary

    Ayushi Dalmia, Jan 23, 2014, in forum: Python
    Replies:
    4
    Views:
    218
    Ayushi Dalmia
    Jan 24, 2014
Loading...