0

For some project I have to make a dictionary in which the keys are urls,among which I have this url:

http://www.microsoft.com/isapi/redir.dll?prd=windows&sbp=mediaplayer&ar=Media&sba=Guide&pver=6.2

the url is too long to fit in here I guess in one single line. I can build a dictionary without any errors this url is also a key. but for some reason when I want to extract the values associated to this key(url). I cannot, I get and error "error key:...." Does someone know what is wrong with this url? Are dictionary keys sensitive to some stuff? thanks

below is the code:

def initialize_sumWTP_table(cursor):
cursor.execute( ''' SELECT url,tagsCount
                     FROM sumWTP''')
rows = cursor.fetchall ()
for url,tagsCount in rows:
    sumWTP[url] = tagsCount
6
  • I bet it's nothing to do with the URL specifically - show us your code. Commented May 16, 2010 at 22:41
  • yes, i fixed it, there is a ? mark. Commented May 16, 2010 at 22:42
  • gotcha, all good now. (It'd still help to see the code though) Commented May 16, 2010 at 22:42
  • also i can easily print the whole dictinary, and also i can access to the items individually, but this key is exception. Commented May 16, 2010 at 22:43
  • I meant all the code. Where's the line that causes the error? Where is sumWTP initialized? Where's the code that adds the URL to the database? If it's a lot of code, you can post it at some other site like pastebin.net (just for example) and link to it, and then just include the most important few lines here. Commented May 16, 2010 at 22:49

4 Answers 4

1

It is almost inconceivable that the dictionary is "losing" your key. I would guess that there is some small change in the string (case, or how the query string is ordered) that results in the same effective URL, but with a slightly different string.

If this is the case, find a way to "normalize" the URL.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem was a bug in my code. I added some exception handling to solve the problem.The data was right, but I have forgotten to do exception handling in these cases. example:

def getWPT(url,tag):
try:
    row = MemoryInitializer.wtp[url][tag]
except KeyError:
    row = 0
#print row
return row

Comments

0

The reason for getting a key error when accessing a dict is that the key does not exist. Verify that the key you think exists, and that you're using the correct string.

3 Comments

i verified it, i can see it there...when I print the whole dictionary i can see it.
i run the code and I can see the keys and values among which is this url, but if I want to access it alone using the key, i can't, other urls work fine.
That's still saying it. Prove it.
0

It's hard to tell based on the code fragment offered. It looks like you are initializing the dictionary. You need to create the sumWTP dictionary first, something like: sumWTP = {}. Then statements like sumWTP[url] = tagcount should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.