我在Mysql数据库中有一个名单列表,我想根据第一个字母对它们进行排序。
当我运行此代码时,我会在报价中打印出句子
import sqlite3
import csv
conn = sqlite3.connect('RefinedDatabase.db')
cursor = conn.cursor()
selectRow = cursor.execute( "SELECT Lecturer FROM staffDataBase")
fetchrow = cursor.fetchone()
while fetchrow != None:
fetchrow = str(fetchrow).replace(',)','').replace('(','').replace('u\'','').replace("'","").replace('u"', '').replace('"','')
fetchrow = 'SELECT lecturer FROM staffDataBase ORDER BY lecturer'
print fetchrow
fetchrow = cursor.fetchone()
conn.close()
print ("All Done!!")
答案 0 :(得分:1)
关注docs
for row in c.execute('SELECT * FROM stocks ORDER BY price'):
print row
会给你
(u'2006-01-05', u'BUY', u'RHAT', 100, 35.14)
(u'2006-03-28', u'BUY', u'IBM', 1000, 45.0)
(u'2006-04-06', u'SELL', u'IBM', 500, 53.0)
(u'2006-04-05', u'BUY', u'MSFT', 1000, 72.0)
这里row
只是一个元组,python允许你解包一个元组。
你可以这样做:
numbers = []
for row in c.execute('SELECT * FROM stocks ORDER BY price'):
date, process,company,number,rate = row
numbers.append(number)
numbers.sort()
print numbers
输出:
[100, 500, 1000, 1000]
如果要使用已排序的元组更新表,则可以执行以下操作:
from operator import itemgetter
tup = ((u'2006-01-05', u'BUY', u'RHAT', 100, 35.14),(u'2006-03-28', u'BUY', u'IBM', 1000, 45.0), (u'2006-04-06', u'SELL', u'IBM', 500, 53.0), (u'2006-04-05', u'BUY', u'MSFT', 1000, 72.0))
sorted_tup = tuple(sorted(tup, key=itemgetter(3)))
print sorted_tup
然后按照this回答
中列出的步骤操作