When you use a repeating statement in Python, there are multiple values in one variable I wonder if it is possible to use the format function specified by one variable when printing.
import sqlite3
class test():
def __init__(self):
self.conn = sqlite3.connect('test.db')
self.curosr = self.conn.cursor()
self.conn.execute('create table if not exists test1(id integer primary key autoincrement, name char(5), age char(5))')
self.conn.execute('create table if not exists test2(id integer primary key autoincrement, name char(5), age char(5))')
self.conn.execute('create table if not exists test3(id integer primary key autoincrement, name char(5), age char(5))')
self.conn.execute("insert into test1(id, name, age) values(null, 'andy', '29')")
self.conn.execute("insert into test1(id, name, age) values(null, 'terry', '30')")
self.conn.execute("insert into test2(id, name, age) values(null, 'bab', '11')")
self.conn.execute("insert into test2(id, name, age) values(null, 'john', '24')")
self.conn.execute("insert into test3(id, name, age) values(null, 'intel', '100')")
self.conn.execute("insert into test3(id, name, age) values(null, 'amd', '50')")
for i in range(1, 4):
for j in self.curosr.execute('select *from test{0} order by id desc'.format(i)).fetchmany():
dic={'j{}'.format(i):j}
print("{0},{1},{2} {3},{4},{5} {6},{7},{8}".format(j[0],j[1],j[2],j[0],j[1],j[2],j[0],j[1],j[2]))
test()
-----------print----------
2,terry,30 2,terry,30 2,terry,30
2,john,24 2,john,24 2,john,24
2,amd,50 2,amd,50 2,amd,50
In the example code, print ("{0},{1},{2}{3},{4},{5}{6},{7},{8}".format(j[0],j[1],j[2],j[0],j[0],j[1],j[2],j[0],j[1],j[2]])
When outputting 2,terry,302,john, 2,24> in such a way?
I'm curious.
It's possible
print("{0}, {1}, {2} {0}, {1}, {2}, {0}, {1}, {2}" .format(j[0],j[1],j[2]))
You can do it like this
© 2024 OneMinuteCode. All rights reserved.