试图获取命令行参数以使用我的代码,但出现错误TypeError:“NoneType”对象不是callab

2024-10-04 09:18:43 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好,我对编程和python还很陌生,但这段代码几乎都能正常工作了。我只需要弄清楚如何让命令行中调用的参数与我的代码一起工作。我试过的都不管用,这部分是:

tree.find("./PLANT/[COMMON='%s'" % sys.argv[3]):

这是我的全部密码:

import os
import xml.etree.ElementTree as ET
import sys

# changes to the correct directory of each different environment
os.chdir(os.path.expanduser('~/Desktop'))

file = "plant_catalog.xml"
tree = ET.parse(file)

# Finds section by plant name and adjusts the price according to argument
 for plant in tree.find("./PLANT/[COMMON='%s'" % sys.argv[3]):
    if plant.tag == "PRICE":
        plant.text = float(plant.text) * sys.argv[4]
        plant.text = str(plant.text)
    print(plant.text)

tree.write("plant_catalog.xml")

以下是引发的错误:

Traceback (most recent call last): File "C:/Users/cader/PycharmProjects/ModifyML/ModifyML.py", line 14, in for plant in tree.find(name): File "C:\Users\cader\AppData\Local\Programs\Python\Python37\lib\xml\etree\ElementTree.py", line 653, in find return self._root.find(path, namespaces) File "C:\Users\cader\AppData\Local\Programs\Python\Python37\lib\xml\etree\ElementPath.py", line 307, in find return next(iterfind(elem, path, namespaces), None) File "C:\Users\cader\AppData\Local\Programs\Python\Python37\lib\xml\etree\ElementPath.py", line 300, in iterfind result = select(context, result) TypeError: 'NoneType' object is not callable

Process finished with exit code 1

我真的很抱歉,如果这是错误的格式,我创建了一个帐户只是为了问这个问题。你知道吗


Tags: textinpyimporttreesyslinexml
1条回答
网友
1楼 · 发布于 2024-10-04 09:18:43

我的印象是ET.parse(file)的结果是一个空指针(没有结果)。你知道吗

我建议你检查一下parse()函数。如果有问题,可以编辑问题并添加该函数的源代码。你知道吗

相关问题 更多 >