简体   繁体   English

如何从 Python 上的 Discord 中的 SQLite 表制作多行列表?

[英]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的表:

emp_id || emp_id || emp_name || emp_name || emp_position emp_position

emp_1 || emp_1 || Jane ||简|| emp_pos1 emp_pos1

emp_2 || emp_2 || Jake ||杰克|| emp_pos2 emp_pos2

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.

相关问题 如何在不使用列表的情况下在 python 中进行多行输入? - How do I make a multi-line input in python without using a list? 如何在 Python 3 中的 assertRegex 中表达多行正则表达式? - How can I express a multi-line regex in assertRegex in Python 3? 如何从 Python 文件中读取多行列表? - How do I read a multi-line list from a file in Python? 如何删除表 header 中的空行和 sphinx_rtd_theme 中的多行单元格? - How can I remove empty line in table header and multi-line cell in sphinx_rtd_theme? 从列表中读取标签时,我不知道如何设置多行 xticklabels - matplotlib - I can't figure out how to set multi-line xticklabels when reading labels from a list - matplotlib 如何将多行Python代码转换为单行? - How can I convert multi-line Python code to a single line? 如何将多行输入存储到列表中(python) - How to store multi-line input into a list (python) 如何将长长的python列表格式化为Fortran多行数组? - How to format a long python list into a Fortran multi-line array? 如何在终端中使用 python 编写多行代码? - How can I write multi-line code in the Terminal use python? 如何在Python中关闭多行变量/注释? - How should I close a multi-line variable/comment in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM