Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 16th, 2012, 12:15 PM
Murtasma Murtasma is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 Murtasma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 45 sec
Reputation 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?

Reply With Quote
  #2  
Old November 16th, 2012, 12:43 PM
Purity_Lake Purity_Lake is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 33 Purity_Lake User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 25 m 17 sec
Reputation Power: 2
what error are you getting?

Reply With Quote
  #3  
Old November 16th, 2012, 12:45 PM
Murtasma Murtasma is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 Murtasma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 45 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #4  
Old November 16th, 2012, 12:46 PM
SuperOscar SuperOscar is online now
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 6 m 41 sec
Reputation Power: 65
Quote:
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 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)

Reply With Quote
  #5  
Old November 16th, 2012, 12:59 PM
Murtasma Murtasma is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 Murtasma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 45 sec
Reputation Power: 0
Quote:
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Can't get items added to dictionary probably easy fix

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap