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
  open a text file using list() Pedroski55 2 116 Feb-25-2026, 06:57 PM
Last Post: noisefloor
  If I open a file write or append, is the file loaded into RAM? Pedroski55 11 1,118 Jan-14-2026, 07:49 AM
Last Post: Pedroski55
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 7,273 Dec-16-2025, 05:29 PM
Last Post: zodazy
Question [SOLVED] Linefeed when writing "f" strings to text file? Winfried 5 858 Nov-04-2025, 11:51 AM
Last Post: buran
  .xlsm file can't be opened after writing data to one worksheet mhyatt 4 885 Oct-11-2025, 11:25 PM
Last Post: Pedroski55
  how to write/overwrite data in a txt. file according to inp Quinn 2 1,587 Aug-12-2025, 04:20 PM
Last Post: Quinn
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 13 36,670 May-20-2025, 12:26 PM
Last Post: hanmen9527
  Replace values in Yaml file with value in dictionary PelleH 1 3,818 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 2,253 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  Problems writing a large text file in python Vilius 4 2,033 Dec-21-2024, 09:20 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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