我正在从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