如果格式化字符串,是否多次替换相同的值



如果值为123,我只需要将字体名称替换为RSQFont,如果值不是,则替换为Regular。。所以我有这个代码。。。

if 123.value:
FontName='RSQFont'
else:
FontName='Regular'
"""<screen backgroundColor="#16000000" name="AGC_Picon" position="210,130" size="800,470" title="Quick Signal Info" zPosition="1" flags="wfNoBorder">
<widget source="Title" render="Label" font="%(key)s;23" foregroundColor="#00bbbbbb" position="0,0" size="350,30" transparent="1" />
<widget source="global.CurrentTime" render="Label" position="545,0" size="250,30" font="%(key)s;23" valign="top" halign="left" foregroundColor="#00bbbbbb" transparent="1">
<convert type="ClockToText">Format:%d-%m-%Y   %H:%M:%S</convert>
</widget>
<widget source="session.CurrentService" render="Label" position="599,403" size="200,25" font="%(key)s; 20" halign="center" backgroundColor="#54111112" foregroundColor="#fec000" transparent="1">
<convert type="RaedQuickServName2">%F %p %Y %M %s</convert>
</widget>
<widget source="session.CurrentService" render="Label" position="599,435" size="200,23" font="%(key)s; 18" halign="center" backgroundColor="#54111112" foregroundColor="#00bbbbbb" transparent="1">
<convert type="RaedQuickServName2">%c %l %h %m %g %b %e %S</convert>
</widget>
<widget name="Satfinder" position="5,319" size="300,18" zPosition="1" font="%(key)s;17" halign="left" backgroundColor="#54111112" foregroundColor="#0000deff" transparent="1" />
</screen>""" % {'key': FontName,}

但是我有这个错误

""" % {'key': FontName,}
TypeError: not enough arguments for format string

我尝试了不同的方法,但我解决不了。。。

我试过

.format(FontName)

而不是

{'key': FontName,}

还有其他事情,但没有任何帮助。。。和建议?!!!

附言:我不能使用%s代码,因为有些行已经有了,并将其与其他python文件一起使用,就像这个一样

<convert type="RaedQuickServName2">%F %p %Y %M %s</convert>

<convert type="ClockToText">Format:%d-%m-%Y   %H:%M:%S</convert>

如注释中所述-使用f-string:

font_name='RSQFont'
html = f"""<screen backgroundColor="#16000000" name="AGC_Picon" position="210,130" size="800,470" title="Quick Signal Info" zPosition="1" flags="wfNoBorder">
<widget source="Title" render="Label" font="{font_name};23" foregroundColor="#00bbbbbb" position="0,0" size="350,30" transparent="1" />
<widget source="global.CurrentTime" render="Label" position="545,0" size="250,30" font="{font_name};23" valign="top" halign="left" foregroundColor="#00bbbbbb" transparent="1">
<convert type="ClockToText">Format:%d-%m-%Y   %H:%M:%S</convert>
</widget>
<widget source="session.CurrentService" render="Label" position="599,403" size="200,25" font="{font_name}; 20" halign="center" backgroundColor="#54111112" foregroundColor="#fec000" transparent="1">
<convert type="RaedQuickServName2">%F %p %Y %M %s</convert>
</widget>
<widget source="session.CurrentService" render="Label" position="599,435" size="200,23" font="{font_name}; 18" halign="center" backgroundColor="#54111112" foregroundColor="#00bbbbbb" transparent="1">
<convert type="RaedQuickServName2">%c %l %h %m %g %b %e %S</convert>
</widget>
<widget name="Satfinder" position="5,319" size="300,18" zPosition="1" font="{font_name};17" halign="left" backgroundColor="#54111112" foregroundColor="#0000deff" transparent="1" />
</screen>"""
print(html)

str.format()方法:

font_name='RSQFont'
html = """<screen backgroundColor="#16000000" name="AGC_Picon" position="210,130" size="800,470" title="Quick Signal Info" zPosition="1" flags="wfNoBorder">
<widget source="Title" render="Label" font="{font};23" foregroundColor="#00bbbbbb" position="0,0" size="350,30" transparent="1" />
<widget source="global.CurrentTime" render="Label" position="545,0" size="250,30" font="{font};23" valign="top" halign="left" foregroundColor="#00bbbbbb" transparent="1">
<convert type="ClockToText">Format:%d-%m-%Y   %H:%M:%S</convert>
</widget>
<widget source="session.CurrentService" render="Label" position="599,403" size="200,25" font="{font}; 20" halign="center" backgroundColor="#54111112" foregroundColor="#fec000" transparent="1">
<convert type="RaedQuickServName2">%F %p %Y %M %s</convert>
</widget>
<widget source="session.CurrentService" render="Label" position="599,435" size="200,23" font="{font}; 18" halign="center" backgroundColor="#54111112" foregroundColor="#00bbbbbb" transparent="1">
<convert type="RaedQuickServName2">%c %l %h %m %g %b %e %S</convert>
</widget>
<widget name="Satfinder" position="5,319" size="300,18" zPosition="1" font="{font};17" halign="left" backgroundColor="#54111112" foregroundColor="#0000deff" transparent="1" />
</screen>""".format(font=font_name) # or .format(**{'font':font_name})
print(html)

您还可以使用jinja2这样的模板引擎来寻找更复杂的解决方案,这是web开发中事实上的标准方法

相关内容

  • 没有找到相关文章

最新更新