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

  • Home
  • SEARCH
  • 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 7048229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:51:59+00:00 2026-05-28T02:51:59+00:00

I am new to Python. Now I have to replace a number of values

  • 0

I am new to Python. Now I have to replace a number of values in an XML file with Python. The example snippet of XML is:

<gmd:extent>
    <gmd:EX_Extent>
      <gmd:description gco:nilReason="missing">
        <gco:CharacterString />
      </gmd:description>
      <gmd:geographicElement>
        <gmd:EX_GeographicBoundingBox>
          <gmd:westBoundLongitude>
            <gco:Decimal>112.907</gco:Decimal>
          </gmd:westBoundLongitude>
          <gmd:eastBoundLongitude>
            <gco:Decimal>158.96</gco:Decimal>
          </gmd:eastBoundLongitude>
          <gmd:southBoundLatitude>
            <gco:Decimal>-54.7539</gco:Decimal>
          </gmd:southBoundLatitude>
          <gmd:northBoundLatitude>
            <gco:Decimal>-10.1357</gco:Decimal>
          </gmd:northBoundLatitude>
        </gmd:EX_GeographicBoundingBox>
      </gmd:geographicElement>
    </gmd:EX_Extent>
  </gmd:extent>

What I want to do is to replace those decimal values, i.e. 112.907, with a specified value.

<gmd:extent>
    <gmd:EX_Extent>
      <gmd:description gco:nilReason="missing">
        <gco:CharacterString />
      </gmd:description>
      <gmd:geographicElement>
        <gmd:EX_GeographicBoundingBox>
          <gmd:westBoundLongitude>
            <gco:Decimal>new value</gco:Decimal>
          </gmd:westBoundLongitude>
          <gmd:eastBoundLongitude>
            <gco:Decimal>new value</gco:Decimal>
          </gmd:eastBoundLongitude>
          <gmd:southBoundLatitude>
            <gco:Decimal>new value</gco:Decimal>
          </gmd:southBoundLatitude>
          <gmd:northBoundLatitude>
            <gco:Decimal>new value</gco:Decimal>
          </gmd:northBoundLatitude>
        </gmd:EX_GeographicBoundingBox>
      </gmd:geographicElement>
    </gmd:EX_Extent>
  </gmd:extent>

I tried with a few methods but none of them worked with my assumption that the difficulty is with the namespace prefix gmd and gco.

Please help me out. Thanks in advance!

Cheers, Alex

  • 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-28T02:52:00+00:00Added an answer on May 28, 2026 at 2:52 am

    I couldn’t get lxml to process your xml without adding fake namespace declarations at the top so here is how your input looked

    <gmd:extent xmlns:gmd="urn:x:y:z:1" xmlns:gco="urn:x:y:z:1">
        <gmd:EX_Extent>
            <gmd:description gco:nilReason="missing">
                <gco:CharacterString />
            </gmd:description>
            <gmd:geographicElement>
                <gmd:EX_GeographicBoundingBox>
                    <gmd:westBoundLongitude>
                        <gco:Decimal>112.907</gco:Decimal>
                    </gmd:westBoundLongitude>
                    <gmd:eastBoundLongitude>
                        <gco:Decimal>158.96</gco:Decimal>
                    </gmd:eastBoundLongitude>
                    <gmd:southBoundLatitude>
                        <gco:Decimal>-54.7539</gco:Decimal>
                    </gmd:southBoundLatitude>
                    <gmd:northBoundLatitude>
                        <gco:Decimal>-10.1357</gco:Decimal>
                    </gmd:northBoundLatitude>
                </gmd:EX_GeographicBoundingBox>
            </gmd:geographicElement>
        </gmd:EX_Extent>
    </gmd:extent>
    

    I assumed you have two lists one for the current values and one for the new ones like this

    old = [112.907, 158.96, -54.7539, -10.1357]
    new = [1,2,3,4]
    d = dict(zip(old,new))

    Here is the full code

    #!/usr/bin/env python
    import sys
    from lxml import etree
    
    def process(fname):
        f = open(fname)
        tree = etree.parse(f)
        root = tree.getroot()
        old = [112.907, 158.96, -54.7539, -10.1357]
        new = [1,2,3,4]
        d = dict(zip(old,new))
        nodes = root.findall('.//gco:Decimal', root.nsmap)
        for node in nodes:
            node.text = str(d[float(node.text)])
        f.close()
        return etree.tostring(root, pretty_print=True)
    
    def main():
        fname = sys.argv[1]
        text = process(fname)
        outfile = open('out.xml', 'w+')
        outfile.write(text)
        outfile.close()
    
    if __name__ == '__main__':
        main()
    

    and here is how the output looked like

    <gmd:extent xmlns:gmd="urn:x:y:z:1" xmlns:gco="urn:x:y:z:1">
        <gmd:EX_Extent>
            <gmd:description gco:nilReason="missing">
                <gco:CharacterString/>
            </gmd:description>
            <gmd:geographicElement>
                <gmd:EX_GeographicBoundingBox>
                    <gmd:westBoundLongitude>
                        <gco:Decimal>1</gco:Decimal>
                    </gmd:westBoundLongitude>
                    <gmd:eastBoundLongitude>
                        <gco:Decimal>2</gco:Decimal>
                    </gmd:eastBoundLongitude>
                    <gmd:southBoundLatitude>
                        <gco:Decimal>3</gco:Decimal>
                    </gmd:southBoundLatitude>
                    <gmd:northBoundLatitude>
                        <gco:Decimal>4</gco:Decimal>
                    </gmd:northBoundLatitude>
                </gmd:EX_GeographicBoundingBox>
            </gmd:geographicElement>
        </gmd:EX_Extent>
    </gmd:extent>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am really new to Python and I have been looking for an example
Im new using Python, now I have a problem using ElementTree and read out
I am new to python and I have a list of years and values
I'm new to Python (I have been programming in Java for multiple years now
I'm new to python, and have a list of longs which I want to
So I am relatively new to python, and in order to learn, I have
I'm new to Python and I'm starting a mini Project, but I have some
Newbie question. Pylons 1 + SQLA using declarative style. New to Python. I have
I'm very new to Python and I have a problem which I thought I
I'm relatively new to python, have done it a bit for my physics university

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.