0

Hey guys just a small question on this connection here. I wanted to print the output of the sql db using python which is currently successful but the output is not what i desired.

def query(conn):
    x = conn.cursor()
    x.execute ("SELECT Amount from Expenses")
for item in x.fetchall():
    items = map(int,item)
    print "The amount is %s" % (items)

output: The amount is [15000] --> which is the concern here. I want to get rid of the square brackets in the output. Please help me out on this

3
  • 1
    items[0]?.... Commented May 30, 2018 at 5:57
  • Hi Rakesh, didn't quite understand it. Please elaborate. Commented May 30, 2018 at 6:00
  • @BhargavMg items is a list "[]" these brackets in python means it's a list. In rakesh's comment he's trying to access the first element from the list. Commented May 30, 2018 at 6:10

2 Answers 2

1

The response you are getting if for items which can be multiple and it return a list.

So you are getting [15000]

How can i get rid of square bracket.

simple items[0]

If you are expecting multiple value and want to get all just simple iterate over a loop.

for item in items:
    print item
Sign up to request clarification or add additional context in comments.

Comments

0

fetchall return you a list of tuples. You do not need map also. You can directly access the element using index.

Ex:

def query(conn):
    x = conn.cursor()
    x.execute ("SELECT Amount from Expenses")
    for item in x.fetchall():
        #items = int(item[0]) -->Use index to access the element. 
        print "The amount is %s" % (item[0])

2 Comments

Hi Rakesh thanx for the help, much appreciated. I actually used map since the int value was being appended with a long extension 15000L. Hence i used it to convert the value to a plain int. Can you please help me understand how even after removing that map command too the L is not appended to the output.
Because L is just a representation for "Long".

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.