Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7194215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:19:35+00:00 2026-05-28T20:19:35+00:00

Hi I have started learning python and want to use it to do something

  • 0

Hi I have started learning python and want to use it to do something to a XML file with.

I have been looking for information on the best course to follow but frankly I got a little lost. There are so many ways of manipulating XML files like ElementTree, lxml,minidom etc, etc, . Could someone point me into the right direction to go. Or point me to some code I can wrap my head around. I have started experimenting with lxml but haven’t gotten any further then printing all elements yet.

Here is what I am trying to do :

  1. Read a line from the csv file. Load in Label and FullPath.
  2. Look in XML file for ITEM with mathing FullPath
  3. Change the FLAG1 for that ITEM to TRUE
  4. Change the FLAG2 and FLAG3 for that ITEM to FALSE
  5. Change the Label for that ITEM to the Label from the CSV file.
  6. Write out new.xml

Below is my xml structure. The two records below repeat like 10000 times in the file.

<ThisIsMyData>
  <ITEM>
    <Number>0</Number>
    <Flag1>TRUE</Flag1>
    <Flag2>FALSE</Flag2>  
    <Flag3>FALSE</Flag3>
    <Label>RED</Label> <<-2- After finding 1 I need to change THIS(only this)
    <Path>C:\\test\\</Path> <-1- I need to find this 
    <file>test.png</file>
  </ITEM>
  <ITEM>
    <Number>1</Number>
    <Flag1>TRUE</Flag1>
    <Flag2>FALSE</Flag2>
    <Flag3>FALSE</Flag3>
    <Label>Blue</Label>
    <Path>c:\\test\\test2\\</Path>
    <file>blue.png</file>
  </ITEM>
 </ThisIsMyData>

So I have a ROOT :
then lot of Elements: .
Each of them have 7 SubElements.

This is what my CSV file looks like and what I need my output to look like :
CSV File :

  Label;FullPath
  YELLOW;C:\\test\\test.png
  YELLOW;c:\\test\\test2\\blue.png

 <ThisIsMyData>
  <ITEM>
    <Number>0</Number>
    <Flag1>FALSE</Flag1>
    <Flag2>FALSE</Flag2>
    <Flag3>TRUE</Flag3>
    <Label>YELLOW</Label>
    <Path>C:\\test\\</Path>
    <file>test.png</file>
  </ITEM>
  <ITEM>
    <Number>1</Number>
    <Flag1>FALSE</Flag1>
    <Flag2>FALSE</Flag2>
    <Flag3>TRUE</Flag3>
    <Label>YELLOW</Label>
    <Path>c:\\test\\test2\\</Path>
    <file>blue.png</file>
  </ITEM>
 </ThisIsMyData>

Pastebin link in case layout gets messed up :

http://pastebin.com/embed_js.php?i=QEx2ZGuY

I am trying ElementTree right now using this example :
http://pymotw.com/2/xml/etree/ElementTree/parse.html. I have managed to search in the xml for a certain element name and print the contents. But I still do not see a way of finding a matching element on the same level.

from xml.etree import ElementTree
with open('mydata.xml', 'rt') as f:
    tree = ElementTree.parse(f)
#    filelist = ElementTree.ElementTree.find()
for node in tree.findall('.//file'):
    FileName = node.tag, node.text
    print FileName      

Output :

('file', 'test.png')
('file', 'blue.png')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:19:36+00:00Added an answer on May 28, 2026 at 8:19 pm

    Here’s a quick example of how to do what I think you want using lxml.etree and xpath.

    from cStringIO import StringIO
    from lxml import etree
    
    xmlfile = StringIO("""
    <ThisIsMyData>
      <ITEM>
        <Number>0</Number>
        <Flag1>TRUE</Flag1>
        <Flag2>FALSE</Flag2>  
        <Flag3>FALSE</Flag3>
        <Label>RED</Label>
        <Path>C:\\test\\</Path>
        <file>test.png</file>
      </ITEM>
      <ITEM>
        <Number>1</Number>
        <Flag1>TRUE</Flag1>
        <Flag2>FALSE</Flag2>
        <Flag3>FALSE</Flag3>
        <Label>Blue</Label>
        <Path>c:\\test\\test2\\</Path>
        <file>blue.png</file>
      </ITEM>
     </ThisIsMyData>
    """.strip())
    
    datafile = StringIO("""
    Label;FullPath
    YELLOW;C:\\test\\test.png
    YELLOW;c:\\test\\test2\\blue.png
    """.strip())
    
    # Read "csv". Simple, no error checking, skip first line.
    filenameToLabel = {}
    for l,f in (x.strip().split(';') for x in datafile.readlines()[1:]):
      filenameToLabel[f] = l
    
    def first(seq,default=None):
      """xpath helper function"""
      for item in seq:
        return item
      return None
    
    doc = etree.XML(xmlfile.read())
    
    for item in doc.xpath('//ITEM'):
      item_filename = first(item.xpath('./Path/text()'),'').strip() + first(item.xpath('./file/text()'),'').strip()
      label = filenameToLabel.get(item_filename)
      if label is not None:
        first(item.xpath('./Flag1')).text = 'TRUE'
        first(item.xpath('./Flag2')).text = 'FALSE'
        first(item.xpath('./Flag3')).text = 'FALSE'
        first(item.xpath('./Label')).text = label
    
    print etree.tostring(doc)
    

    Yields

    <ThisIsMyData>
      <ITEM>
        <Number>0</Number>
        <Flag1>TRUE</Flag1>
        <Flag2>FALSE</Flag2>
        <Flag3>FALSE</Flag3>
        <Label>YELLOW</Label>
        <Path>C:\test\</Path>
        <file>test.png</file>
      </ITEM>
      <ITEM>
        <Number>1</Number>
        <Flag1>TRUE</Flag1>
        <Flag2>FALSE</Flag2>
        <Flag3>FALSE</Flag3>
        <Label>YELLOW</Label>
        <Path>c:\test\test2\</Path>
        <file>blue.png</file>
      </ITEM>
    </ThisIsMyData>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started learning ruby and i have been looking at ruby on rails.Is
I have started learning Ruby on Rails and I have been stuck at something
I have just started learning NHibernate. Over the past few months I have been
I've been a web developer for some time now, and have recently started learning
I've just started learning Python today. I've been reading a Byte of Python. Right
I have recently started learning Python and I have 2 questions relating to modules.
I have just started learning Lucene and would like to use it for indexing
I have started learning Python by writing a small application using Python 3.1 and
I have started learning EJB. I like to know which is best application server
I have just started learning python and am getting caught up. I come from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.