python-3.x - 从 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>
解决方案
循环遍历您的记录列表并获取所需的属性,然后将其添加到字符串中
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)
推荐阅读
- php - 使用 Magento 2.3.3 p1 后面临的问题
- java - java中的不可变函数参数
- php - 如何设置用 PHP(使用 Xampp)创建的 Web 应用程序?
- firebase - createUserWithPhone 可能吗?扑
- php - 使用条件在 PHP 中加载 Div HTML 代码
- python - 如何使用内置输入函数评估 .py 文件?
- docker - Traefik v2 不使用路径路由但使用 ip
- php - 插入数据库失败,带有文件附件
- java - 将 Jfoenix 9 控件与 JDK 13 一起使用时出错
- android - 如何在安卓中启用“在锁定屏幕中显示”权限