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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:15:51+00:00 2026-05-31T00:15:51+00:00

After reading from an existing file with ‘ugly’ XML and doing some modifications, pretty

  • 0

After reading from an existing file with ‘ugly’ XML and doing some modifications, pretty printing doesn’t work. I’ve tried etree.write(FILE_NAME, pretty_print=True).

I have the following XML:

<testsuites tests="14" failures="0" disabled="0" errors="0" time="0.306" name="AllTests">
    <testsuite name="AIR" tests="14" failures="0" disabled="0" errors="0" time="0.306">
....

And I use it like this:

tree = etree.parse('original.xml')
root = tree.getroot()

...    
# modifications
...

with open(FILE_NAME, "w") as f:
    tree.write(f, pretty_print=True)
  • 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-31T00:15:52+00:00Added an answer on May 31, 2026 at 12:15 am

    For me, this issue was not solved until I noticed this little tidbit here:

    http://lxml.de/FAQ.html#why-doesn-t-the-pretty-print-option-reformat-my-xml-output

    Short version:

    Read in the file with this command:

    >>> parser = etree.XMLParser(remove_blank_text=True)
    >>> tree = etree.parse(filename, parser)
    

    That will “reset” the already existing indentation, allowing the output to generate it’s own indentation correctly. Then pretty_print as normal:

    >>> tree.write(<output_file_name>, pretty_print=True)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to create xml after reading from a datatable, in C# application. This
After reading an article about the subject from O'Reilly , I wanted to ask
After reading this answer: best way to pick a random subset from a collection?
After reading this description of late static binding (LSB) I see pretty clearly what
After searching and reading about Modern OpenGL in order to upgrade my existing project,
When reading data from the Input file I noticed that the ¥ symbom was
I'm writing a basic program that copies a string from an existing text file
I am new to Mercurial / TortoiseHG / VirtualHG. After reading some tutorials I
After reading the Head First Design Patterns book and using a number of other
After reading this question , I was reminded of when I was taught Java

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.