简体   繁体   中英

Replace the same value multiple if formatting a string?

I need just to replace the name of font as RSQFont if value 123 or Regular if not value.. So I have this code...

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,}

But I have got this error

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

I tried different method but I can not solve it...

I have tried

 .format(FontName)

instead of

{'key': FontName,}

And other things but nothing to help... and advice?!!!

Ps: I cannot use %s code because some lines already have and use it with other python files as like this

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

and

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

As already mentioned in the comments - use 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)

or str.format() method:

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)

You can also go for a more complex solution using template engine like jinja2 , which is de-facto standard approach in web-development

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM