0

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 ?!!!

P.s: 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>
9
  • 2
    why not use f-string or str.format(), instead of old-style string formatting or even template engine like jinja2? Commented Mar 20, 2021 at 14:39
  • Perhaps it's a posting problem, but the "code" in the first part of your question is invalid and doesn't make sense. Does it contain a triple-quoted string somewhere? Commented Mar 20, 2021 at 15:20
  • 1
    Now it makes more sense. The problem is because the string contains % characters which Python's % string operator interprets. To work around it you would need to "escape" all the other % characters by doubling them (so the % string operator could turn them back into single characters). A simpler workaround would be to use the more modern str.format() method that uses { and } characters (as @buran suggested already). Commented Mar 20, 2021 at 16:18
  • 1
    Another alternative (that also uses { and } characters) would be to use f-strings which were added in Python 3.6. Commented Mar 20, 2021 at 16:32
  • 1
    If you used font="{0};23" you would only have to pass FontName once. Commented Mar 20, 2021 at 16:34

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.