0
我正在讀取一個xml文件,並且想要對節點的內容執行字符串操作。Python中xml節點上的字符串操作
import os
import elementtree.ElementTree as ET
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import tostring
xml_file = os.path.abspath(__file__)
xml_file = os.path.dirname(xml_file)
xml_file = os.path.join(xml_file, "Small1Review.xml")
print xml_file
root = ET.parse(xml_file).getroot()
text = tostring(root)
#print text
for a in text:
#print a, "-->", a.text
text = tostring(a)
print text
但代碼提供了以下錯誤,
Traceback (most recent call last):
File "myEtXML.py", line 33, in <module>
text = tostring(a)
File "C:\Python26\lib\xml\etree\ElementTree.py", line 1009, in tostring
ElementTree(element).write(file, encoding)
File "C:\Python26\lib\xml\etree\ElementTree.py", line 543, in __init__
assert element is None or iselement(element)
AssertionError
我如何解析每個節點和每個人進行一些字符串操作?