0

Here prsnson.result contains the data which are comming from an query. I want the expected output, but its not coming. I am using python 2.5

for row in prsnobj.result:
    ansdb = {row[0] : row[1]}
    print ansdb

Actual: {1L: 3L} {2L: 4L} {3L: 2L} {4L: 2L} {5L: 2L}

Expected: {1: 3, 2: 4, 3: 2, 4: 2, 5: 2}

2

3 Answers 3

1

Integers fetched from a database is commonly returned as Long when utilizing the different database interfaces.

If you want the output you're after

ansdb = {}
for row in prsnobj.result:
    ansdb[int(row[0])] = int(row[1])

Perhaps the MySQL documentation for Python could help clear things up

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

4 Comments

OP wants a single dict, not multiple dicts.
I wouldn't say always - it depends on the interface you are using
yes i dont want multiple dictionary...i want is all in one dictionary
Man, people here are starting to get lazy. One would think that the actual problem was getting ints from the longs and appending unique keys to a dict would be the easy task.
0
for row in prsnobj.result:
    ansdb = {int(row[0]) : int(row[1])}
print ansdb

you result type is long, you should tranfer to int

1 Comment

i dnt want multiple dictionary
0

All the L means is that it's in long integer (64-bit) form, so your returned value is indeed still correct. When you create a new dictionary with entries already in there (as you are), it's going to automatically convert all of the entries to long form; instead you should use a tuple or simply print each one in the loop itself:

for row in prsnobj.result:
    print row[0] + " : " + row[1]

or

for row in prsnobj.result:
    ansdb = (row[0], row[1])
    print ansdb

Depending on how your prsnobj object was defined, you may end up getting long integers still. If so, you need to look back at the code that defines prsnobj and check that it was created properly. Use printouts to make sure that the object is always in the state that you want it to be in!

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.