Não sei exatamente o que você está procurando como um resultado final, mas aqui está um script Python que converterá seus dados em XML:
#!/usr/bin/env python2
# -*- coding: ascii -*-
"""transform.py
Parses a data file contain textual records in the following format:
[RecordUUID.n]
"Variable1Key"="Variable1Value"
"Variable2Key"="Variable2Value"
"Variable3Key"="Variable3Value"
and converts it to an XML document with record-elements of the following form:
<RecordUUID.1>
<Variable1Key>Variable1Value</Variable1Key>
<Variable2Key>Variable2Value</Variable2Key>
<Variable3Key>Variable3Value</Variable3Key>
</RecordUUID.1>
"""
import sys
import re
from xml.etree.ElementTree import Element, SubElement, tostring
from xml.dom.minidom import parseString
# Creat a root element for the XML document
root = Element('root')
# Set a variable to keep track of the current record
current_record = None
# Parse the data and construct an XML representation
with open(sys.argv[1]) as datafile:
# Extract the non-empty lines from the data file
lines = [line.strip() for line in datafile if line.strip()]
# Iterate over the lines
for line in lines:
# Check to see if we've reached a new record
if "RecordUUID" in line:
# Extract the record ID
eid = line.strip()[1:-1]
# Add a new child element to the document
# and update the current record
current_record = SubElement(root, eid)
# Check to see if we've reached a new key-value pair
else:
match = re.match(r'^"(\w+)"="(\w+)"$', line.strip())
# If we have a key-value pair then update the current record
if match:
key, value = match.groups()
SubElement(current_record, key).text = value
# Display the generated XML document
print(parseString(tostring(root)).toprettyxml(indent="\t"))
Se colocarmos os seguintes dados (ou seja, os dados de exemplo da sua pergunta) em um arquivo chamado data.txt
:
[RecordUUID.1]
"Variable1Key"="Variable1Value"
"Variable2Key"="Variable2Value"
"Variable3Key"="Variable3Value"
[RecordUUID.4]
"Variable1Key"="Variable1Value"
"Variable5Key1"="Variable51Value1"
"Variable5Key1"="Variable51Value2"
"Variable5Key2"="Variable52Value1"
"Variable5Key2"="Variable52Value2"
E, em seguida, execute o script:
user@host:~$ python transform.py data.txt
Em seguida, obtemos a seguinte saída:
<?xml version="1.0" ?>
<root>
<RecordUUID.1>
<Variable1Key>Variable1Value</Variable1Key>
<Variable2Key>Variable2Value</Variable2Key>
<Variable3Key>Variable3Value</Variable3Key>
</RecordUUID.1>
<RecordUUID.4>
<Variable1Key>Variable1Value</Variable1Key>
<Variable5Key1>Variable51Value1</Variable5Key1>
<Variable5Key1>Variable51Value2</Variable5Key1>
<Variable5Key2>Variable52Value1</Variable5Key2>
<Variable5Key2>Variable52Value2</Variable5Key2>
</RecordUUID.4>
</root>