用于字典的Python split()
我尝试从字典中的数据库中插入一些值,它工作正常,但它以这种格式插入值,例如用于字典的Python split(),python,dictionary,split,Python,Dictionary,Split,我尝试从字典中的数据库中插入一些值,它工作正常,但它以这种格式插入值,例如“S”而不是S 我尝试使用函数split(),如示例所示: cat_sName = dumps(item_meta_data[int(menu_item.id)]['short_name'].split('"')) 当我打印cat_sName时,它会打印这个值: ['','']而不是S您可以使用strip而不是split来去除引号: >>> '"S"'.strip('"') 'S' 它们最初来自哪里?
“S”
而不是S
我尝试使用函数split()
,如示例所示:
cat_sName = dumps(item_meta_data[int(menu_item.id)]['short_name'].split('"'))
当我打印cat_sName时,它会打印这个值:
['','']
而不是S
您可以使用strip
而不是split
来去除引号:
>>> '"S"'.strip('"')
'S'
它们最初来自哪里?试试以下方法:
cat_sName = dumps(item_meta_data[int(menu_item.id)]['short_name'].split('"')[1])
您应该使用strip()
函数删除双引号:
像这样
cat_sName = dumps(item_meta_data[int(menu_item.id)]['short_name'].strip('"'))
strip
函数删除内容,而split
函数根据分隔符将字符串拆分为一个列表。您应该使用strip来执行删除操作谢谢,它可以与strip()配合使用