首页 > 解决方案 > 从 sql 输出更改格式

问题描述

我正在编写一个 discord bot rn,我想以特定格式从列中输出所有值。

@commands.command()
@commands.has_guild_permissions(administrator=True)
async def listchannel(self, ctx):
    if ctx.author == bot.user:
        return

    guild_id = str(ctx.guild.id)

    channel = await bot.pg_con.fetch("SELECT channel_id FROM anticrash WHERE guild_id = $1", guild_id)

    if not channel:
        await ctx.send(f'No channel was not added to this server: {guild_id}')
        return
    
    await ctx.send(f"{channel}")

输出是这样的

[<Record channel_id='847854890985980005'>, <Record channel_id='847546806937059341'>]

但我想将输出更改为这种格式

<#847854890985980005>, <#847546806937059341>

标签: python-3.xpostgresqldiscord.py

解决方案


循环遍历您的记录列表并获取所需的属性,然后将其添加到字符串中

message = ""

for record in channel:
    message += f"<#{record['channel_id']}> ,"

await ctx.send(message)

如果你想要一个单行:

message = ', '.join([f"<#{record['channel_id']}>" for record in channel])
await ctx.send(message)

推荐阅读