#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0

    Question Can't get items added to dictionary probably easy fix


    Code:
    import csv
    file_shortcode = "some file"
    
    rFile = open(file_shortcode,"rb")
    reader = csv.reader(rFile)
    rownum = 0
    shortcode = {}
    for row in reader:
        #save header row.
        if rownum != 0:
            name = str(row[0])
            code = row[1]
            print name
            print code
            shortcode[str(row[0])] = str(row[1])
    print row[0] + " " + row[1]
    rownum += 1
    rFile.close()
    for item in shortcode:
        print shortcode[item]
    print len(shortcode)
    The following line is giving me issues
    Code:
    shortcode[str(row[0])] = str(row[1])
    I can't seem to figure out why I can't add a key and value to my dictonary object doing this. I've testing added a item like this
    Code:
    shortcode["user"] = "code"
    and that works just fine however when I try and stick the same data from my csv file into the dictonary object nothing gets added. The code below
    Code:
            name = str(row[0])
            code = row[1]
    also does not appear to work but the print statement does
    Code:
    print row[0] + " " + row[1]
    what am I doing wrong?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    33
    Rep Power
    3
    what error are you getting?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by Purity_Lake
    what error are you getting?
    Sorry forgot to mention. I'm not getting an error there just isn't any data in the dictionary when there should be.
    Code:
    for item in shortcode:
        print shortcode[item]
    print len(shortcode)
    This cold should display the values but it displays nothing. Len(shortcode) returns 0.

    if I add something manually however len produces 1
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    438
    Rep Power
    67
    Originally Posted by Murtasma
    Code:
    for row in reader:
        #save header row.
        if rownum != 0:
            name = str(row[0])
            code = row[1]
            print name
            print code
            shortcode[str(row[0])] = str(row[1])
    print row[0] + " " + row[1]
    rownum += 1
    what am I doing wrong?
    Unless you copied-and-pasted wrong, “rownum += 1” is not inside the loop and thus only gets updated once.

    Anyway, you’d be better off using the enumerate() function:

    Code:
    for rownum, row in enumerate(reader):
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by SuperOscar
    Unless you copied-and-pasted wrong, “rownum += 1” is not inside the loop and thus only gets updated once.

    Anyway, you’d be better off using the enumerate() function:

    Code:
    for rownum, row in enumerate(reader):
    Stupid tabs that's was the problem. The code is working now much appreciated!

IMN logo majestic logo threadwatch logo seochat tools logo