Python 如果格式化字符串,是否将相同的值替换为倍数?

Python 如果格式化字符串,是否将相同的值替换为倍数?,python,Python,如果值为123,我只需要将字体名称替换为RSQFont,如果值不是,则替换为Regular。。 所以我有这个密码 if 123.value: FontName='RSQFont' else: FontName='Regular' """<screen backgroundColor="#16000000" name="AGC_Picon" position="210,130" siz

如果值为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,}
我尝试了不同的方法,但我不能解决它

我试过了

 .format(FontName)
而不是

{'key': FontName,}
和其他事情,但没有帮助。。。 还有建议

p.s:我不能使用%s代码,因为有些行已经有了%s代码,并将其与其他python文件一起使用,如下所示

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

格式:%d-%m-%Y%H:%m:%S

如注释中所述-使用f字串:

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)

您还可以使用模板引擎(如
jinja2
)来寻找更复杂的解决方案,这是web开发中事实上的标准方法,如注释中所述-使用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)

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

为什么不使用f-string或
str.format()
),而不是旧式的字符串格式,甚至是模板引擎(如
jinja2
)?也许这是一个发布问题,但是“代码”你问题的第一部分是无效的,没有意义。它是否在某个地方包含三重引号字符串?现在它更有意义了。问题是因为字符串包含Python的
%
字符串运算符解释的
%
字符。要解决此问题,您需要通过将所有其他
%
字符加倍来“转义”它们(因此
%
字符串运算符可以将它们转换回单个字符)。一个更简单的解决方法是使用更现代的
str.format()
方法,该方法使用
{
}
字符(正如@buran已经建议的那样)。另一个替代方法(也使用
{
}
字符)是使用Python 3.6中添加的字符。如果使用
font=“{0};23”
您只需通过
FontName
一次。为什么不使用f-string或
str.format()
,而不是旧式的字符串格式,甚至像
jinja2
这样的模板引擎?也许这是一个发布问题,但问题的第一部分中的“代码”无效且没有意义。它是否在某个地方包含三重引号字符串?现在它更有意义了。问题是因为字符串包含Python的
%
字符串运算符解释的
%
字符。要解决此问题,您需要通过将所有其他
%
字符加倍来“转义”它们(因此
%
字符串运算符可以将它们转换回单个字符)。一个更简单的解决方法是使用更现代的
str.format()
方法,该方法使用
{
}
字符(正如@buran已经建议的那样)。另一个替代方法(也使用
{
}
字符)是使用Python 3.6中添加的字符。如果使用
font=“{0};23”
您只需通过
FontName
一次。
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)
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)