当前位置: 动力学知识库 > 问答 > 编程问答 >

how to print the sqlite values in a nice format with python?

问题描述:

OK, so I am pulling some data from a sqlite database and I can't figure out how to properly format them when showing to the users...I'm using python 2.5.

 studentid = raw_input("Enter your student ID: ")

cur.execute('SELECT APP_DECISION FROM APP_STATUS WHERE STUDENTID = ?', (studentid,))

appdecision = cur.fetchall()

cur.execute('SELECT GRANT FROM FINAID WHERE STUDENTID = ?', (studentid,))

grant = cur.fetchall()

if (appdecision == 'ACCEPTED'):

print 'congratulations'

else:

print 'sorry'

print (appdecision)

print (grant)

the results are getting printed like so,

Enter your student ID: 100006

[(u'ACCEPTED',)]

[(9500,)]

I want to be able to print it such as Accepted, and $9500....also. How do I check for the specific value so I can congratulate the users which have been accepted?

Thanks so much!

网友答案:

Print it as:

print (appdecision[0][0])
print (grant[0][0])

This is because [(u'ACCEPTED',)] is a list that contains a tuple that contains the string "ACCEPTED". Similarly, you'll have to check for a value like this:

if appdecision[0][0] == "ACCEPTED":
    print "Congratulations!"
分享给朋友:
您可能感兴趣的文章:
随机阅读: