ElementTree in Python 2.6.2 Processing Instructions support?

后端 未结 5 710
隐瞒了意图╮
隐瞒了意图╮ 2020-12-11 06:12

I\'m trying to create XML using the ElementTree object structure in python. It all works very well except when it comes to processing instructions. I can create a PI easil

5条回答
  •  無奈伤痛
    2020-12-11 06:38

    f = open('D:\Python\XML\test.xml', 'r+')
    old = f.read()
    f.seek(44,0)      #place cursor after xml declaration
    f.write(''+ old[44:])
    

    I was facing the same problem and came up with this crude solution after failing to insert the PI into the .xml file correctly even after using one of the Element methods in my case root.insert (0, PI) and trying multiple ways to cut and paste the inserted PI to the correct location only to find the data to be deleted from unexpected locations.

提交回复
热议问题