Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ElementTree写入方法意外返回TypeError_Python_Elementtree - Fatal编程技术网

Python ElementTree写入方法意外返回TypeError

Python ElementTree写入方法意外返回TypeError,python,elementtree,Python,Elementtree,我正在尝试使用以下函数将XML文件写入磁盘: def modify_xml(input_file_xml, output_file_xml, input_file_names, output_path, output_format, params_sub_nodes = {}): tree = ET.parse(input_file_xml) root = tree.getroot() if len(input_file_names) == 1: file

我正在尝试使用以下函数将XML文件写入磁盘:

def modify_xml(input_file_xml, output_file_xml, input_file_names, output_path, output_format, params_sub_nodes = {}):
    tree = ET.parse(input_file_xml)
    root = tree.getroot()
    if len(input_file_names) == 1:
        file_node_input = root.find('node[@id="Read"]/parameters/file')
    elif len(input_file_names) == 2:
        file_node_input = root.find('node[@id="ProductSet-Reader"]/parameters/fileList')
    else:
        raise Exception('number of files equal to: %s not supported !' %str(len(input_file_names)))
    file_node_input.text = ','.join(input_file_names)

    file_node_output_name = root.find('node[@id="Write"]/parameters/file')
    file_node_output_name.text = output_path

    file_node_output_format = root.find('node[@id="Write"]/parameters/formatName')
    file_node_output_format.text = output_format

    for sub_node_name, sub_node_content in params_sub_nodes.items():
        sub_node = root.find(sub_node_name)
        sub_node.text = sub_node_content

    print(output_file_xml)
    print(type(output_file_xml))
    tree.write(output_file_xml)
但我得到以下类型错误:

in modify_xml
    tree.write(output_file_xml)
  File "/usr/lib/python3.6/xml/etree/ElementTree.py", line 776, in write
    short_empty_elements=short_empty_elements)
  File "/usr/lib/python3.6/xml/etree/ElementTree.py", line 941, in _serialize_xml
    short_empty_elements=short_empty_elements)
  File "/usr/lib/python3.6/xml/etree/ElementTree.py", line 941, in _serialize_xml
    short_empty_elements=short_empty_elements)
  File "/usr/lib/python3.6/xml/etree/ElementTree.py", line 941, in _serialize_xml
    short_empty_elements=short_empty_elements)
  File "/usr/lib/python3.6/xml/etree/ElementTree.py", line 938, in _serialize_xml
    write(_escape_cdata(text))
TypeError: write() argument must be str, not tuple
即使在打印输出文件xml及其类型(函数中最后三行)时,我发现它是一个字符串:

/tmp/filter_edited.xml
<class 'str'>
/tmp/filter\u edited.xml
即使程序因此错误而崩溃,XML也会创建并保存在指定的输出路径中
您认为我在这里遗漏了什么?

在彻底调试我的代码后,我找到了问题的原因

在/usr/lib/python3.6/xml/etree/ElementTree.py的第938行设置断点,并逐个可视化插入树中的元素类型(xml函数中的文本变量),结果发现其中一个是元组

这是因为在调用modify_xml函数时,我错误地在output_path变量的末尾添加了一个逗号,该函数将字符串转换为元组

file_node_output_name.text = output_path

遗憾的是,错误消息没有太多表现力!我的意思是,当读到这个错误时:

TypeError: write() argument must be str, not tuple

首先想到的是,XML的输出路径(write的函数参数)中存在类型错误,而XML本身中插入的文本中没有类型错误…

您确定代码中没有两次调用该函数吗?它可能在第一次通话中成功,在第二次通话中失败?@Grismar,事实上,这正是发生的事情。如果这回答了问题,请随意写下你自己的答案并加以解释并接受,或者关闭/删除该问题,因为确实不需要回答。希望有帮助。