我正在從MySQL數據庫中讀取數據並希望將其打印到屏幕上。python tuple中mysql的格式化數據
我用:
import MySQLdb
conn = MySQLdb.connect(host='localhost',user='root',passwd='passwd',db='test',charset='utf8')
cursor = conn.cursor()
cursor.execute('select cust_name,cust_address,cust_city,cust_state from Customers')
numrows = int(cursor.rowcount)
for i in range(numrows):
row = cursor.fetchone()
print "%s %s %s %20s" %(row[0],row[1],row[2],row[3])
cursor.close()
conn.close()
的結果是這樣的:
Village Toys 200 Maple Lane Detroit MI Kids Place 333 South Lake Drive Columbus OH Fun4All 1 Sunny Place Muncie IN Fun4All 829 Riverside Drive Phoenix AZ The Toy Store 4545 53rd Street Chicago IL
我怎樣才能使它左對齊?
非常感謝,我想得到每一行的理由 – user1773219
@ user1773219請把結果通緝的問題。在評論中,格式化會丟失。 – ovgolovin
非常感謝。我很抱歉,我不能清楚地表達這個問題。我認爲這個答案很棒('{row [0]:20} {row [1]:20} {row [2]:20} {row [3]:20}'。format(row = row)) – user1773219