试着自学Python和Sqlite,我的头在旋转。如何“清理”查询的输出以去掉所有括号、逗号等.从结果来看。也想.title()的第二栏。例如:
def get_all_bdays(self):
print("\n" * 100)
print("The birthdays we know about are: ")
self.c.execute('SELECT * FROM birthdays')
for row in self.c.fetchall():
print(row)下列产出的结果:
The birthdays we know about are:
(1, 'joe smoe', '12-12-1212')一个人如何将这种混乱重新整理成这样的东西:
The birthdays we know about are:
1. Joe Smoe 12-12-1212我的最终目标是为我的小企业创建一个库存系统,我的员工可以使用这个系统来查找库存物品在我的库房中的位置。考虑用水瓶做类似的事情,但我离那个时间点还有很长的路要走。
发布于 2017-04-12 02:20:27
每一行都是一个包含三个值的元组:数字、名称和生日。print(row)输出的是元组,包含所有括号和引号,而不是任何格式化版本。
在Python中,您可以解构元组并将其部分分配给不同的变量,然后使用Python的printf-like格式语法进行格式化:
for row in self.c.fetchall():
number, name, date = row
print("%d. %s on %s" % (number, name.title(), date))甚至:
for number, name, date in self.c.fetchall:
print("%d. %s on %s" % (number, name.title(), date))发布于 2017-04-12 02:08:44
当您print(row)时,您将得到row的Python表示,其中包括引号和逗号等。您想要做的是将数据str.format为您喜欢的任何形状:
fmt = "{0}. {1}, on {2}"
for row in self.c.fetchall():
print(fmt.format(*row))https://stackoverflow.com/questions/43359006
复制相似问题