3

i have xml (template) file and i set variables in the xml using {$var}

and i have a list with values for the xml variables.

how can i open (set) my file and set the xml variable to the values from my list ?

<sut>
    <Cdu>
        <class>XXX</class>
        <conn>
            <class>XXX</class>
            <cli>
                <password>{$password}</password>
                <user>{$user}</user>
                <host>{$host}</host>
                <connectOnInit>false</connectOnInit>
            </cli>
        </conn>
        <basic>
            <class>XXX</class>
        </basic>
    </Cdu>
1
  • 1
    Have a look at the Mako template framework. Commented Jul 13, 2021 at 9:15

3 Answers 3

4

Why not just use f string?

password = 'secret'
user = 'jack'
host = '12.56.78.123'
xml = f'''<sut>
    <Cdu>
        <class>XXX</class>
        <conn>
            <class>XXX</class>
            <cli>
                <password>{password}</password>
                <user>{user}</user>
                <host>{host}</host>
                <connectOnInit>false</connectOnInit>
            </cli>
        </conn>
        <basic>
            <class>XXX</class>
        </basic>
    </Cdu>'''
print(xml)

output

<sut>
    <Cdu>
        <class>XXX</class>
        <conn>
            <class>XXX</class>
            <cli>
                <password>secret</password>
                <user>jack</user>
                <host>12.56.78.123</host>
                <connectOnInit>false</connectOnInit>
            </cli>
        </conn>
        <basic>
            <class>XXX</class>
        </basic>
    </Cdu>
Sign up to request clarification or add additional context in comments.

3 Comments

He has <password>{$password}</password> in input not <password>{password}</password>
The question is - can he control the template and modify it. If he can - f string is a perfect win, else - we need more complex solution. @undertale - do you have control over the "template"?
good news - just use my code and you are done :-)
2

I would use re.sub for this task following way

import re
txt = '''<password>{$password}</password>
<user>{$user}</user>
<host>{$host}</host>'''
data = {"password": "SECRET", "user": "NAME", "host": "LOCAL"}
def replacement(x):
    return data[x.group(1)]
out = re.sub(r'\{\$([^\}]+)\}', replacement, txt)
print(out)

output

<password>SECRET</password>
<user>NAME</user>
<host>LOCAL</host>

Explanation: re.sub second argument might be function which accept Match object. I used regular expression with one capturing group, which content I then use to look up data dict. Note that { and $ and } need escaping as these have special meaning, but we need literal { and $ and }. Disclaimer: I used substring of original for brevity sake.

Comments

2

You can use Template class from standard string module:

from string import Template


password = 'secret'
user = 'jack'
host = '12.56.78.123'

xml_template = '''<sut>
    <Cdu>
        <class>XXX</class>
        <conn>
            <class>XXX</class>
            <cli>
                <password>${password}</password>
                <user>${user}</user>
                <host>${host}</host>
                <connectOnInit>false</connectOnInit>
            </cli>
        </conn>
        <basic>
            <class>XXX</class>
        </basic>
    </Cdu>'''

xml = xml_template.substitute(password=password, user=user, host=host)
print(xml)

There are also safe_substitute method that may be used in case you are not sure about context variables.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.