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
  Write sql data or CSV Data into parquet file mg24 2 103 Sep-26-2022, 08:21 AM
Last Post: ibreeden
  Saving the print result in a text file Calli 8 225 Sep-25-2022, 06:38 PM
Last Post: snippsat
  Need Help: Convert .pcl file to .pdf file ManuRaval 6 272 Sep-13-2022, 01:31 PM
Last Post: ManuRaval
  Use module docx to get text from a file with a table Pedroski55 8 328 Aug-30-2022, 10:52 PM
Last Post: Pedroski55
  Graphic line plot with matplotlib, text file in pytho khadija 2 366 Aug-15-2022, 12:00 PM
Last Post: khadija
  read a text file, find all integers, append to list oldtrafford 12 745 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 310 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  Exporting Stock Fundamental Data to a CSV file with yahoo_fin DustinKlent 2 3,297 Aug-01-2022, 06:08 PM
Last Post: paulyan
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 294 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  what will be the best way to find data in txt file? korenron 2 284 Jul-25-2022, 10:03 AM
Last Post: korenron

Forum Jump:

User Panel Messages

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