没关系 - 我发现了我真正的问题,我意识到它在我的代码中更进一步。
我在xml.etree.ElementTree
像我期望的那样上班时遇到了问题。
xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><suggestedmatches><destination><sortOrder>1</sortOrder><destinationType>destinationType1</destinationType></destination><destination><sortOrder>2</sortOrder><destinationType>destinationType2</destinationType></destination></suggestedmatches>"
root = ET.fromstring(xmlData)
logging.debug("DIAG: %s: root.tag = %s"
% (FUNCTION_NAME, root.tag))
logging.debug("DIAG: %s: root = %r" % (FUNCTION_NAME, ET.tostring(root)))
destinations = root.findall("destination")
logging.debug('DIAG: %s: destinations = %r' % (FUNCTION_NAME, ET.tostring(destinations)))
我想弄清楚为什么我找不到destinations
在root
。
DEBUG:root:DIAG: findDestinations(): root.tag = suggestedmatches
DEBUG:root:DIAG: findDestinations(): root = b'<suggestedmatches><destination><sortOrder>1</sortOrder><destinationType>destinationType1</destinationType></destination><destination><sortOrder>2</sortOrder><destinationType>destinationType2</destinationType></destination></suggestedmatches>'
ERROR:root:findDestinations(): Encountered exception on root.findall() - 'list' object has no attribute 'iter'
如果我在 get 之后添加以下代码,我会root
看到日志中列出的每个目的地:
for destination in root:
destinationList.append(destination)
logging.debug('DIAG: %s: destination.tag = %s'
% (FUNCTION_NAME, destination.tag))
同样的代码在不同的脚本中工作,所以我不确定为什么它在这里不起作用。
你得到None
是因为ET.dump
写入sys.stdout
并且你正在记录它的返回dump
是None
.
从文档:
xml.etree.ElementTree.dump(elem)
将元素树或元素结构写入 sys.stdout。此功能应仅用于调试。
确切的输出格式取决于实现。在这个版本中,它被写成一个普通的 XML 文件。
elem 是元素树或单个元素。
尝试使用tostring
method 而不是dump
.
logging.debug("DIAG: %s: root = %r" % (FUNCTION_NAME, ET.tostring(root)))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句