首页 > 解决方案 > 如何摆脱多余的括号、引号或逗号 Python

问题描述

我正在制作一个不和谐的机器人,但在获取没有额外括号、引号或逗号的列表值时遇到了问题。

这是代码:

#gets slots for page
    def listindexcheck(slot):
        if totalitemcount > slot + (page * 9):
            return slot + (page * 9)
        else:
            return 0

    if argument == 'show':
#message set up
        embed = discord.Embed(
            title='title',
            description='Here is your inventory',
            colour=discord.Colour.red())
        for i in range(9):
            embed.add_field(name=f"Slot ({listindexcheck(i+1)})", value=f'Dur: {str(item_dur[listindexcheck(i+1)])}\n'
                                                                        f'Mod: {str(item_mod[listindexcheck(i+1)])}\n'
                                                                        f'E.lvl: {str(item_level[listindexcheck(i+1)])}\n'
                                                                        f'*id:{str(item_ids[listindexcheck(i+1)])}*\n')
        embed.set_footer(text='page: ' + str(page+1))
        msg = await ctx.send(embed=embed)
        print (type(str(item_ids[listindexcheck(i+1)])))

这是输出

将其转换为字符串之前的数据类型是列表

我试图将值转换为字符串类型以至少去掉引号,但这没有用

我的问题是有没有办法只获得价值而不做任何额外的事情?

谢谢

标签: python-3.xdiscord.py

解决方案


将其转换为字符串后,您可以使用 replace() 方法删除您不想要的任何额外部分。您可以将不需要的任何内容替换''为简单地将其从字符串中删除。

以下是有关 replace() 方法的一些信息:https ://www.w3schools.com/python/ref_string_replace.asp


推荐阅读