Python Forum
Modify values in XML file by data from text file (without parsing)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modify values in XML file by data from text file (without parsing)
#1
Hi, I have 2 types of files - one is base XML file where are some attribute values = "FAIL" and i the other file - text file where are values I want to use instead of those "FAIL".
Picture below explain visualy what I mean:

[Image: 31R1A.png]

I need somehow to put in in correct place and I have no idea how to do it. I have been thinking about formating this text file to another XML file and then parse them both and replace value but I has to be done easier, and It would be perfect if it is possible without parsing XML file because it destroy structure of it (deletes whitespaces, sort attributes etc.). I tought about using regex to first find in text file Object id (number before "=>"), then attribute name (string between "=>" and ":") and then find value (string between Attribute name + ":" and "<END_OF_OBJECT_LINE>" or "<COMMA>") and somehow put in in XML but also I am not sure how to do it.

XML file:

    <Module bs="Mainfile_1">
    <object id="1000" name="namex" number="1">
        <item name="item0" value="100" />
        <item name="item00" value="100" />
    </object>
    <object id="1001" name="namey" number="2">
        <item name="item1" value="100" />
        <item name="item00" value="100" />
    </object>
    <object id="1234" name="name1" number="3">
        <item name="item1" value="FAIL" />
        <item name="item2" value="233" />
        <item name="item3" value="233" />
        <item name="item4" value="FAIL" />
    </object>
    <object id="1238" name="name2" number="4">
        <item name="item8" value="FAIL" />
        <item name="item9" value="233" />
    </object>
    <object id="2345" name="name32" number="5">
        <item name="item1" value="111" />
        <item name="item2" value="FAIL" />
    </object>
    <object id="2347" name="name4" number="6">
        <item name="item1" value="FAIL" />
        <item name="item2" value="FAIL" />
        <item name="item3" value="233" />
        <item name="item4" value="FAIL" />
    </object>
    </Module>
Text file:

   1234=>item1:something11:
    
    something11<COMMA>item4:something12:
    
    12something<END_OF_OBJECT_LINE>
    1238=>item8:something12:
    
    something11:<END_OF_OBJECT_LINE>
    2345=>item2:something12:
    
    something11:<END_OF_OBJECT_LINE>
    1234=>item1:something1:
    
    something11<COMMA>item2:something:
    
    11something<COMMA>item4:something:
    
    11something<END_OF_OBJECT_LINE>
Reply
#2
I'm not really buying your arguments for not wanting to parse the XML. Why do you think the whitespace and order of attributes matter?
Reply
#3
(Apr-13-2022, 04:26 AM)ndc85430 Wrote: I'm not really buying your arguments for not wanting to parse the XML. Why do you think the whitespace and order of attributes matter?

Because I have many files like that and it need to be uploaded in software which shows requirements for software testing, and not formating it is quite iportant because it must be as clear to read as possible. Albo I want to compare edited and original file if everything is correct, but when parsing destroys the whole structure it would be impossible to check every difference in file with +100k lines
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 27,977 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  Replace values in Yaml file with value in dictionary PelleH 1 2,127 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  How to write variable in a python file then import it in another python file? tatahuft 4 903 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  Problems writing a large text file in python Vilius 4 979 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  Get an FFMpeg pass to subprocess.PIPE to treat list as text file? haihal 2 1,008 Nov-21-2024, 11:48 PM
Last Post: haihal
  parsing a tree of text first the right most aligned blocks of text and so on arvindikchari 2 765 Nov-21-2024, 01:42 AM
Last Post: BashBedlam
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,051 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 901 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  To fetch and iterate data from CSV file using python vyom1109 3 1,002 Aug-05-2024, 10:05 AM
Last Post: Pedroski55
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 6,273 Jun-17-2024, 12:26 AM
Last Post: AdamHensley

Forum Jump:

User Panel Messages

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