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
は、どのように私は、各ノードを解析し、それらのそれぞれの上にいくつかの文字列操作を行うことができますか。?