小写python字符串,引用字典
我有一本字典,小写python字符串,引用字典,python,Python,我有一本字典,值。在以下方面: <cleared_for_sale>%(cleared_for_sale)s</cleared_for_sale>""" % values %(已清除待售)s“”%value cleared\u for_sale变量是True或False。但是,我需要将其显示为True或False。如何将其小写?我不想更改正在使用的字符串格式类型(我不能使用“字符串”%str(var)。lower()如何 (“”“%”(已清除待售)s“”%value.
值
。在以下方面:
<cleared_for_sale>%(cleared_for_sale)s</cleared_for_sale>""" % values
%(已清除待售)s“”%value
cleared\u for_sale
变量是True
或False
。但是,我需要将其显示为True
或False
。如何将其小写?我不想更改正在使用的字符串格式类型(我不能使用“字符串”%str(var)。lower()
如何
(“”“%”(已清除待售)s“”%value.lower()
或许
str(值)。替换(“T”、“T”)。替换(“F”、“F”)
因为您没有解释为什么不能使用str(values).lower()…这两种解决方案都回答了您的问题,因为您目前的措辞是在进行格式化之前,请将字典中的值从布尔值更改为字符串:
values['cleared_for_sale'] = str(values['cleared_for_sale']).lower()
还有一种选择,因为您对str(var)有些反感。lower()
:
现在,当在格式字符串上使用
%值时,%(已清除待售)s
将被true
或false
代替true
或false
,而不修改任何其他行为。为什么不能使用str(var).lower()
?为什么不能使用str(var).lower()
?@Blender+1用于&apos;:)
values['cleared_for_sale'] = 'true' if values['cleared_for_sale']) else 'false'