【问题标题】:change format from sql output从 sql 输出更改格式
【发布时间】:2021-08-16 23:34:35
【问题描述】:

我正在编写一个不和谐的机器人程序,我想以特定格式从列中输出所有值。

@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.x postgresql discord.py


    【解决方案1】:

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

    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)
    

    【讨论】:

    • 所以我把代码放进去,我得到一个模块错误“TypeError: 'module' object is not iterable”
    • 完整请回溯
    • 啊,是的,对不起,你是怎么定义这个[&lt;Record channel_id='847854890985980005'&gt;, &lt;Record channel_id='847546806937059341'&gt;]的?是channel吗?
    • 是的,频道是默认的。我曾经从 sql 中选择它
    猜你喜欢
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多