格式化一行XML时出现问题

时间:2019-02-24 21:37:16

标签: python xml python-2.7

我试图格式化一行XML文件并失败。

即我尝试了几种方法来获得最新的效果

gdml_pretty = ET.tostring(gdml,encoding='utf8').decode('utf8')
xmlstr = minidom.parseString(gdml_pretty).prettyxma(indet=" ")
print xmlstr

但是他们全都在意念通话中崩溃

关于我要去哪里的任何想法。系统是Ubuntu 18.04

received signal SIGSEGV, Segmentation fault.
#0  /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20) [0x7f5d5e1a6f20]
#1  /lib/x86_64-linux-gnu/libexpat.so.1(XML_SetHashSalt+0x1b) [0x7f5d535dcfcb]
#2  /usr/lib/python2.7/lib-dynload/pyexpat.x86_64-linux-gnu.so(+0x6865) [0x7f5cd20f8865]
#3  /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x6ff3) [0x7f5d5fd8f1b3]
#4  /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x8b5b) [0x7f5d5fd90d1b]
#5  /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x8b5b) [0x7f5d5fd90d1b]
#6  /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x8b5b) [0x7f5d5fd90d1b]
#7  /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x7d8) [0x7f5d5fec0278]
#8  /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x5bf6) [0x7f5d5fd8ddb6]
#9  /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x7d8) [0x7f5d5fec0278]
#10  /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x5bf6) [0x7f5d5fd8ddb6]
#11  /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x8b5b) [0x7f5d5fd90d1b]
#12  /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x7d8) [0x7f5d5fec0278]
#13  /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalCode+0x19) [0x7f5d5fd88029]
#14  /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyRun_StringFlags+0x76) [0x7f5d5fe2b546]
#15  0x7f5d6030ab46 in Base::InterpreterSingleton::runString[abi:cxx11](char const*) from /usr/lib/freecad/lib/libFreeCADBase.so+0x66
#16  0x7f5d60e0aac3 in Gui::Command::runCommand(Gui::Command::DoCmd_Type, char const*) from /usr/lib/freecad/lib/libFreeCADGui.so+0x53
#17  0x7f5d60d9be7b in Gui::Application::exportTo(char const*, char const*, char const*) from /usr/lib/freecad/lib/libFreeCADGui.so+0x3fb
#18  0x7f5d60e16716 in StdCmdExport::activated(int) from /usr/lib/freecad/lib/libFreeCADGui.so+0x576
#19  0x7f5d60e0ef0b in Gui::Command::invoke(int) from /usr/lib/freecad/lib/libFreeCADGui.so+0x7b
#20  0x7f5d5ec98f70 in QMetaObject::activate(QObject*, QMetaObject const*, int, void**) from /usr/lib/x86_64-linux-gnu/libQtCore.so.4+0x4d0
#21  0x7f5d5f1aadc2 in QAction::triggered(bool) from /usr/lib/x86_64-linux-gnu/libQtGui.so.4+0x42
#22  0x7f5d5f1ac113 in QAction::activate(QAction::ActionEvent) from /usr/lib/x86_64-linux-gnu/libQtGui.so.4+0x83
#23  /usr/lib/x86_64-linux-gnu/libQtGui.so.4(+0x62236d) [0x7f5d5f60e36d]
#24  /usr/lib/x86_64-linux-gnu/libQtGui.so.4(+0x626759) [0x7f5d5f612759]
#25  0x7f5d5f207930 in QWidget::event(QEvent*) from /usr/lib/x86_64-linux-gnu/libQtGui.so.4+0x2a0
#26  0x7f5d5f61696b in QMenu::event(QEvent*) from /usr/lib/x86_64-linux-gnu/libQtGui.so.4+0x6b
#27  0x7f5d5f1b103c in QApplicationPrivate::notify_helper(QObject*, QEvent*) from /usr/lib/x86_64-linux-gnu/libQtGui.so.4+0x8c
#28  0x7f5d5f1b8136 in QApplication::notify(QObject*, QEvent*) from /usr/lib/x86_64-linux-gnu/libQtGui.so.4+0x476
#29  0x7f5d60de8958 in Gui::GUIApplication::notify(QObject*, QEvent*) from /usr/lib/freecad/lib/libFreeCADGui.so+0x88
#30  0x7f5d5ec848ad in QCoreApplication::notifyInternal(QObject*, QEvent*) from /usr/lib/x86_64-linux-gnu/libQtCore.so.4+0x8d
#31  0x7f5d5f1b773d in QApplicationPrivate::sendMouseEvent(QWidget*, QMouseEvent*, QWidget*, QWidget*, QWidget**, QPointer<QWidget>&, bool) from /usr/lib/x86_64-linux-gnu/libQtGui.so.4+0x14d
#32  /usr/lib/x86_64-linux-gnu/libQtGui.so.4(+0x249ddc) [0x7f5d5f235ddc]
#33  0x7f5d5f234ce3 in QApplication::x11ProcessEvent(_XEvent*) from /usr/lib/x86_64-linux-gnu/libQtGui.so.4+0x16b3
#34  /usr/lib/x86_64-linux-gnu/libQtGui.so.4(+0x272592) [0x7f5d5f25e592]
#35  /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0(g_main_context_dispatch+0x2e7) [0x7f5d57834387]
#36  /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0(+0x4c5c0) [0x7f5d578345c0]
#37  /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0(g_main_context_iteration+0x2c) [0x7f5d5783464c]
#38  0x7f5d5ecb520e in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) from /usr/lib/x86_64-linux-gnu/libQtCore.so.4+0x7e
#39  /usr/lib/x86_64-linux-gnu/libQtGui.so.4(+0x272666) [0x7f5d5f25e666]
#40  0x7f5d5ec8312f in QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) from /usr/lib/x86_64-linux-gnu/libQtCore.so.4+0x3f
#41  0x7f5d5ec83495 in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) from /usr/lib/x86_64-linux-gnu/libQtCore.so.4+0x1b5
#42  0x7f5d5ec89459 in QCoreApplication::exec() from /usr/lib/x86_64-linux-gnu/libQtCore.so.4+0x99
#43  0x7f5d60da0d16 in Gui::Application::runApplication() from /usr/lib/freecad/lib/libFreeCADGui.so+0x13a6
#44  freecad(main+0x6db) [0x5556082844db]
#45  /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7) [0x7f5d5e189b97]
#46  freecad(_start+0x2a) [0x55560828568a]

1 个答案:

答案 0 :(得分:0)

已经找到了一种无需最小约束即可达到预期结果的方法:向树中添加空格,以便照常保存它会生成漂亮的树。

在此处输入代码`     #就地prettyprint格式化程序

def indent(elem, level=0):
    i = "\n" + level*"  "
    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