Python Forum
Formating generated .data file to XML
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formating generated .data file to XML
#4
Hello, I would like to make un update. I managed to find somekind of solution with regex, but I have one problem. In code below, I can only get as many arguments as many times I wrote "([^:]+):(.+?))?(?:(<COMMA>)", so the regex expects up to 2 <COMMA> instances per "record" which produces up to 3 Attribute elements, but there might be a situation when I have in my file +100 arguments separated by <COMMA>. Do you have any idea how to make it find every argument without writing a mile long line of regex ?

import re
from lxml import etree

root = etree.Element("Module")

with open("datafile.data", "r") as f:
    df = f.read() 
    result = re.finditer(r'(?s)\n?(\d{1,5})=>(?:([^:]+):(.+?))(?:(<COMMA>)([^:]+):(.+?))?(?:(<COMMA>)([^:]+):(.+?))?(<END_OF_OBJECT_LINE>\n)', df)
    for m in result:
        obj = etree.SubElement(root, "Object")
        obj.set("id", m.groups()[0])
        at = etree.SubElement(obj, "Attribute")
        at.set("name",m.groups()[1])
        at.set("value",m.groups()[2])
        for idx in range(len(m.groups())):
            if m.groups()[idx] == '<COMMA>':
                at = etree.SubElement(obj, "Attribute")
                at.set("name",m.groups()[idx + 1])
                at.set("value",m.groups()[idx + 2])

print(etree.tostring(root, pretty_print=True).decode("utf-8"))
Reply


Messages In This Thread
RE: Formating generated .data file to XML - by malcoverc - Apr-14-2022, 09:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  HOW TO USE C# GENERATED DLL davide_vergnani 2 1,710 Jun-12-2023, 03:35 PM
Last Post: davide_vergnani
  propper formating paracelsusx 2 1,941 Jul-16-2021, 09:17 AM
Last Post: perfringo
  Adding graph points and formating project_science 4 2,443 Jan-24-2021, 05:02 PM
Last Post: project_science
  xml file creation from an XML file template and data from an excel file naji_python 1 2,140 Dec-21-2020, 03:24 PM
Last Post: Gribouillis
  How do you work with procedurally generated data? rbbauer00 1 1,509 Jul-08-2020, 04:21 AM
Last Post: ndc85430
  How to save CSV file data into the Azure Data Lake Storage Gen2 table? Mangesh121 0 2,137 Jun-26-2020, 11:59 AM
Last Post: Mangesh121
  Excel: Apply formating of a sheet(file1) to another sheet(file2) lowermoon 1 2,077 May-26-2020, 07:57 AM
Last Post: buran
  the exe file by generated by pyinstaller ,can't get the PYTHONPATH roger2020 11 7,130 Jan-14-2020, 11:07 AM
Last Post: roger2020
  tuple and formating problem darktitan 7 3,478 Feb-17-2019, 07:37 PM
Last Post: marienbad
  Use Variables Generated from Functions in different files to use on the main file AykutRobotics 3 2,995 Jan-01-2019, 04:19 PM
Last Post: AykutRobotics

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020