首页 > 解决方案 > Python“For Loop”在调试模式下工作,但从终端正常运行时却不行?

问题描述

非常业余的问题来了。我有这段代码可以用 Elementtree 解析 XML。然后我运行一个 for 循环来显示所有子标签和属性。如果我在调试中运行它,我可以看到终端中显示的每个嵌套元素和数据。但是,当我使用“shift+enter”运行整个代码时(在此处使用 VSCode)。

它给了我这个错误

(Pdb) for child in root.iter(): * SyntaxError: unexpected EOF while parsing (Pdb) print (child.tag, child.attrib) * NameError: name 'child' is not defined (Pdb)

这是代码。尝试使用谷歌搜索,但我可能使用了错误的关键字。没有找到任何明确的信息。

import xml.etree.ElementTree as ET
tree = ET.parse('90301007.xml')
root = tree.getroot()
receiptid = "74925"
root.tag
root.attrib
for child in root.iter():
    print (child.tag, child.attrib)

标签: pythonelementtree

解决方案


PDB 不适合多行语句。我正在使用 Python 命令通过 powershell 运行代码,现在它工作正常。奇怪的是,逐行调试将允许代码在 PDB 中工作,但不能正常运行。无论哪种方式,我不知道为什么和如何,但是 python 命令在这里可以解决问题。


推荐阅读