python-3.x - 如何摆脱多余的括号、引号或逗号 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)])))
这是输出
将其转换为字符串之前的数据类型是列表
我试图将值转换为字符串类型以至少去掉引号,但这没有用
我的问题是有没有办法只获得价值而不做任何额外的事情?
谢谢
解决方案
将其转换为字符串后,您可以使用 replace() 方法删除您不想要的任何额外部分。您可以将不需要的任何内容替换''
为简单地将其从字符串中删除。
以下是有关 replace() 方法的一些信息:https ://www.w3schools.com/python/ref_string_replace.asp
推荐阅读
- c# - 为什么我们默认允许溢出?
- javascript - JavaScript RegExp 捕获组
- python - 无法在 python 中使用 calcbsimpvol 函数找到隐含波动率
- php - PHP/HTML - 需要 form_1 获取用户名,然后提交后转到 form_2,它会记住用户名并打招呼
- python - 在蟒蛇龟上画一个正方形
- linux - 语法错误:预期操作数(错误标记为“+”)
- r - 要在 PDF 类型 RMD 中获得 2 页中的 huxtable
- php - 通过 Laravel 中的任务计划程序通知
- python - 仅自动将表情符号添加到链接的不和谐机器人
- python - 如何使用openCV保存裁剪的图像?