[英]How can I make the multi-line list from SQLite table in Discord on Python?
I am trying to make bot to send values from SQLite table as a list.我试图让机器人将 SQLite 表中的值作为列表发送。 Here is table called Employees:这是名为Employees的表:
Here is the function for my command to show the emp list:这是我的命令显示 emp 列表的功能:
@bot.command()
async def EmpTest(ctx):
with conn:
cursor.execute("SELECT emp_name, emp_position, emp_id FROM Employees")
emp_list = cursor.fetchall()
for j in range(0):
column = []
for i in range(0):
column += emp_list
column += "\n"
emp_list += column
for column in emp_list:
for item in column:
print(item, end=" ")
print()
await ctx.send(emp_list)
It works perfectly fine when it prints in console:在控制台中打印时它工作得很好:
Jane emp_pos1 emp_1
Jake emp_pos2 emp_2
But that's what Discord gave output after ctx.send(emp_list):
[('Jane', 'emp_pos1', emp_1), ('Jake', 'emp_pos2', emp_2)]
How can I make it to be send in Discord like in console?但这就是 Discord 在ctx.send(emp_list):
之后给出的输出ctx.send(emp_list):
[('Jane', 'emp_pos1', emp_1), ('Jake', 'emp_pos2', emp_2)]
我怎样才能让它像 Discord 一样发送在控制台?
When you're getting a data from SQLite , you get a tuple
type data.当您从SQLite获取数据时,您会得到一个tuple
类型的数据。 In this case, you get a list of tuples.在这种情况下,您将获得一个元组列表。
So at first, you need to get each one of the items of the list.因此,首先,您需要获取列表中的每一项。 You can do that with a for loop
.你可以用for loop
来做到这一点。
for emp_info in emp_list:
print(emp_info)
Simply, you printed 2 employees' information.简单地说,您打印了 2 位员工的信息。
Then, you should delete the parentheses.然后,您应该删除括号。 There are several ways to do that.有几种方法可以做到这一点。 I'll show you one of the ways to do it.我将向您展示其中一种方法。
You can use string.join()
function.您可以使用string.join()
函数。 You can do this:你可以这样做:
for emp_info in emp_list:
print(', '.join(emp_info))
Then you can edit this to your code like this:然后你可以像这样编辑你的代码:
for emp_info in emp_list:
await ctx.send(', '.join(emp_info))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.