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 5838339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:27:26+00:00 2026-05-22T11:27:26+00:00

I have some python xml code using ElementTree that writes a very ugly xml

  • 0

I have some python xml code using ElementTree that writes a very ugly xml file. I wanted to make the xml file a bit more readable. But ElementTree has no pretty-print function. In the documentation ElementTree shows an ‘indent’ method. When I try to use this indent method, I get the following error.

   Traceback (most recent call last):
      File "/cygdrive/c/data/path/myFile.py", line 756, in <module>
          main()
          ...
          self.writeXML()
          File "/cygdrive/c/data/path/myFile.py", line 248, in writeXML
                self.indent(root)
     File "/cygdrive/c/data/path/myFile.py", line 252, in indent

        i = "\n" + level*"  "
        TypeError: object cannot be interpreted as an index


 def writeXML(self):
   root = self.myTree.getroot()
   self.indent(root)
   self.myTree.write(self.myXML)

 def indent(elem, level=0):
     i = "\n" + level*"  "   #Error Here!!
     if len(elem):
         if not elem.text or not elem.text.strip():
             elem.text = i + "  "
         if not elem.tail or not elem.tail.strip():
             elem.tail = i
         for elem in elem:
             indent(elem, level+1)
         if not elem.tail or not elem.tail.strip():
             elem.tail = i
     else:
         if level and (not elem.tail or not elem.tail.strip()):
             elem.tail = i

Am I using indent incorrectly? Or is there an error in this code? Any recommendations for an easier pretty-print?

History: I used to use PyXML which had a pretty-print. But, PyXML is died when I went to python 2.6. lxml has a pretty-print but will not install on my system. So I converted all my code to use ElementTree because I know it works and has most of the basic functionality I need.

  • 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-22T11:27:27+00:00Added an answer on May 22, 2026 at 11:27 am

    You say

    i = "\n" + level*"  "
    TypeError: object cannot be interpreted as an index
    

    but there is nothing in that statement that looks even vaguely like an index operation. Suggestion: without changing your code in any other way, insert

    print repr(elem), repr(level)

    before the above statement and edit your question to show the result. Also add what version of Python, and show how you imported ElementTree (or cElementTree).

    You appear to have copy/pasted a routine from the effbot’s ElementLib … It looks OK apart from the obfuscatory but not fatal for elem in elem.

    One major problem is that for some strange reason you have made it a method of your class instead of a stand-alone function. Either (1) drag it out of your class and call it like

    indent(root)

    or (2) change its definition to

    def indent(self, elem, level=0)

    and see if the problem goes away.

    Update The problem will go away:

    [Python 2.6.6]
    >>> import xml.etree.ElementTree as et
    >>> et.Element('atag') * "   "
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: object cannot be interpreted as an index
    

    The above mysterious error message must have been a bug; Python 2.7.1 produces the much more sensible

    TypeError: can't multiply sequence by non-int of type 'Element'
    

    from the same code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some XML files on my PYTHONPATH that I would like to load
I'm trying to write some xml by this piece of code docs = XmlReportGenerator()
I'm trying to write some xml by this piece of code docs = XmlReportGenerator()
got a question regarding serializing classes that I've defined. I have some classes like
Actually I'm using R + Python with RPY2 to manipulate data and ggplot to
Recently I have become a fan of storing various settings used for my testing
I'm new with python programming and GUI. I search on internet about GUI programming
I'm trying to access some objects inside a ui:binder component, but not sure of
I need to pull some data from the API. It returns the GET in
tl:dr How can I populate jsTree based on a folder location instead of an

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.