简体   繁体   English

如何让pyodbc输出更具可读性/更好?

[英]How do I make pyodbc output more readable/nicer?

I'm currently making a discord bot, and one of it's commands involves pulling data from a SQL table, for this I'm using the AioOdbc module, which almost exactly the same as Pyodbc, with the only real difference being that it doesn't block in asynchronous functions. 我正在制作一个discord bot,其中一个命令涉及从SQL表中提取数据,为此我使用的是AioOdbc模块,它与Pyodbc几乎完全相同,唯一真正的区别在于它没有' t阻塞异步函数。

Which outputs in this format. 以这种格式输出哪些。

[('Item1',),('Item2',)]

How can I have it output something which is a bit nicer to read? 我怎样才能输出一些更好看的内容? Possibly something like 可能是类似的东西

Item1, Item2

Any help is appreciated! 任何帮助表示赞赏!

You can turn it into a string where every item is separated by a comma and space with this code: 您可以将其转换为一个字符串,其中每个项目都使用以下代码用逗号和空格分隔:

itemString = ', '.join((item[0] for item in items))

Where items is the name of this: [('Item1',),('Item2',)] 其中items是其名称: [('Item1',),('Item2',)]

If you need it to be able to take items out from nested lists with arbitrary depth, such as in this list: [('Item1', 'Item2', ('Item3')), ('Item4', 'Item5')] , you can use this code: 如果你需要它能够从具有任意深度的嵌套列表中取出项目,例如在此列表中: [('Item1', 'Item2', ('Item3')), ('Item4', 'Item5')] ,你可以使用这段代码:

from collections import Iterable

def flatten(nested):
    for element in nested:
        if isinstance(element, Iterable) and not isinstance(element, (str, bytes)):
            yield from flatten(element)
        else:
            yield element

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM