Question:
The Python script at the end of its work creates an output document output.txt Below is an abstract example for understanding
<!--vvv Начало шапки документа vvv-->
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="https://www.w3schools.com" xmlns="https://www.w3schools.com" elementFormDefault="qualified" creator="Junior" version="0.00" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://services.agresso.com/schema/ABWInvoice/2011/11/14 http://services.agresso.com/schema/ABWInvoice/2011/11/14/ABWInvoice.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:agrlib = "http://services.agresso.com/schema/ABWSchemaLib/2011/11/14" xmlns:agr = "http://services.agresso.com/schema/ABWInvoice/2011/11/14">
<metadata>
<link href="https://www.w3schools.com"><text>Text for link</text></link>
<time>Date_time</time>
<name>Name_to_display</name>
<extensions>Red</extensions>
</metadata>
<!--^^^ Конец шапки документа ^^^-->
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<!--vvv Ниже, часть документа собираеммая скриптом vvv-->
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
...............
<!--^^^ Конец работы скрипта ^^^-->
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This document has a certain header template (the first part of the document), where only two values need to be inserted, and the rest remains according to the header template.
Значения для замены: <time>Date_time</time> и <name>Name_to_display</name>
I want to know how to format this part of the code correctly.
I have only two options in my head at the moment:
- Complete writing of all lines to a file.
with open('output.txt', 'w', encoding='utf-8') as (file):
file.write('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n')
file.write('<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="https://www.w3schools.com" xmlns="https://www.w3schools.com" elementFormDefault="qualified" creator="Junior" version="0.00" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://services.agresso.com/schema/ABWInvoice/2011/11/14 http://services.agresso.com/schema/ABWInvoice/2011/11/14/ABWInvoice.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:agrlib = "http://services.agresso.com/schema/ABWSchemaLib/2011/11/14" xmlns:agr = "http://services.agresso.com/schema/ABWInvoice/2011/11/14">')
file.write('<metadata>\n<link href="https://www.w3schools.com"><text>Text for link</text></link>\n<time>' + str(Date_time) + '</time>\n<name>' + str(Name_to_display) + '</name>\n<extensions>Red</extensions>\n</metadata>\n')
file.write('<xs:element name="note">\n<xs:complexType>\n<xs:sequence>\n')
for j in range(len(trkpt)):
file.write(trkpt[j] + '\n')
file.write('</xs:sequence>\n</xs:complexType>\n</xs:element>\n</xs:schema>\n')
- Have a ready-made head.txt template.
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="https://www.w3schools.com" xmlns="https://www.w3schools.com" elementFormDefault="qualified" creator="Junior" version="0.00" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://services.agresso.com/schema/ABWInvoice/2011/11/14 http://services.agresso.com/schema/ABWInvoice/2011/11/14/ABWInvoice.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:agrlib = "http://services.agresso.com/schema/ABWSchemaLib/2011/11/14" xmlns:agr = "http://services.agresso.com/schema/ABWInvoice/2011/11/14">
<metadata>
<link href="https://www.w3schools.com"><text>Text for link</text></link>
<time>$metadata_time$</time>
<name>$metadata_name$</name>
<extensions>Red</extensions>
</metadata>
<xs:element name="note">
<xs:complexType>
<xs:sequence>
Reading and replacing values. And further work of the script.
head_file = open('head.txt', 'r', encoding='utf-8').read().replace('$metadata_time$', str(Date_time)).replace('$metadata_name$', str(Name_to_display))
with open('obj/output/output.gpx', 'w', encoding='utf-8') as (file):
file.write(head_file)
for j in range(len(trkpt)):
file.write(trkpt[j] + '\n')
file.write('</xs:sequence>\n</xs:complexType>\n</xs:element>\n</xs:schema>\n')
Answer:
And what if you use standard string formatting (so as not to go to jinja). That is, in the template, replace $metadata_time$ and $metadata_name$ with {metadata_time} and {metadata_name}, respectively, and
data = dict(
metadata_time=str(Date_time),
metadata_name=str(Name_to_display)
)
head_file = open('head.txt', 'r', encoding='utf-8').read().format(**data)