Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make Beautiful Soup keep the attributes ordering and lines indentation

I am using Bueatiful Soup with the XML parser

soup = BeautifulSoup(open("/root/sandbox/garbage/example_jmx.jmx"), "xml")

This keeps the attributes upper\lower case as in the parsed file. (FYI, Using the "lxml" parser results in all the attributes being saved as lower case.)

But I am left with 2 other problems.

  1. The order of the attributes is changed.
  2. The indentation of the lines is lost. All the lines are aligned to the right

e.g.for #1

<TestPlan enabled="true" guiclass="TestPlanGui" testclass="TestPlan">

turns to

<TestPlan guiclass="TestPlanGui" testclass="TestPlan" enabled="true">

How can I keep the original attributes order and lines indentation?

like image 450
RaamEE Avatar asked Dec 12 '25 02:12

RaamEE


1 Answers

  1. the bs4 store tag's attribute in dict object, dict is unordered object.
  2. indentation do not matter in bs4, it uses children parent to show the relationship of tag.

If you want to keep all the indentation and order of attribute, you should open the file directly.

like image 167
宏杰李 Avatar answered Dec 13 '25 14:12

宏杰李



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!