python - 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)
解决方案
PDB 不适合多行语句。我正在使用 Python 命令通过 powershell 运行代码,现在它工作正常。奇怪的是,逐行调试将允许代码在 PDB 中工作,但不能正常运行。无论哪种方式,我不知道为什么和如何,但是 python 命令在这里可以解决问题。
推荐阅读
- python - 抑制 xarray.DataArray.plot 中的标签
- javascript - 将对象数组重新格式化为键数组
- sql - 没有函数,没有Split_String。使用 XML 通过分隔符将字符串拆分为不同的字段
- python - 将列表转换为 NumPy 数组的问题
- c# - Newtonsoft.Json 序列化双重格式化为字符串
- text - H1标签上的文本后面的行
- html - 使用 Sibling Combinator 为动态内容创建选项卡
- java - 在 onOptionsItemSelected 中选择第二个项目会导致应用程序崩溃
- html - 为什么我的 XSL 将兄弟姐妹作为空元素的子元素输出?
- python - 折叠 Pandas 数据框中的行,每列具有不同的逻辑