Skip to main content
 首页 » 编程设计

python之无法从子进程解析 xml。Python 中的 Popen 输出

2025年05月04日14落叶无声

我正在使用这个库来解析我的 xml:

import xml.etree.cElementTree as xml 

解析器的 xml 输入是 subprocess.Popen 的输出:

XMLFile = subprocess.Popen(xml_command,shell=True,stdout=subprocess.PIPE, executable="/bin/ksh").communicate()[0] 
root = xml.XML(XMLFile) 

我收到这个错误:

IOError: [Errno 36] File name too long: ' <?xml version=\...' 

但是,当我将同一命令 xml_command 生成的 xml 作为文件传递时,它工作得很好:

root = xml.parse("/home/test.xml") 

请您参考如下方法:

试试这个:

import xml.etree.cElementTree as xml 
p = subprocess.Popen(xml_command, shell=True, stdout=subprocess.PIPE, executable="/bin/ksh") 
text, err = p.communicate() 
root = xml.fromstring(text) 

使用 python 2.6.6

Store output of subprocess.Popen call in a string