Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.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
从shell-python解析XML_Python_Xml_Shell_Parsing - Fatal编程技术网

从shell-python解析XML

从shell-python解析XML,python,xml,shell,parsing,Python,Xml,Shell,Parsing,我需要从shell解析xml。 当我发出命令时 telnet IP_addr port 因此,从python脚本中,我做到了: subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) 其中cmd是我的命令 我获得了一个必须解析的XML 这是我的输出: Trying 10.1.6.123... Connected to 10.1.6.123. Escape character is '^]'. <?xml version="1.

我需要从shell解析xml。 当我发出命令时

telnet IP_addr port
因此,从python脚本中,我做到了:

subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
其中cmd是我的命令

我获得了一个必须解析的XML 这是我的输出:

Trying 10.1.6.123...
Connected to 10.1.6.123.
Escape character is '^]'.
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="14">
<g>
        <V>13</V>
        <E>34</E>
        <node>
                <name>172.16.102.116</name>
                <id>172.16.102.116</id>
                <regen_pools>
                        <count>0</count>
                        <item_version>0</item_version>
                </regen_pools>
                <osnr_db>0.000000000e+00</osnr_db>
                <osnr>
                        <count>0</count>
                        <item_version>0</item_version>
                </osnr>
        </node>
....
但是xml是作为文件传递的。如何在不创建文件的情况下完成

非常感谢
Silvia

使用

EX:

import xml.etree.ElementTree as ET
tree = ET.ElementTree(ET.fromstring(xmlstring))
示例代码

xmlData = '''<Tables>
<Table><Claimable>false</Claimable><MinorRev>80601</MinorRev><Operation>530600 ION MILL</Operation><HTNum>162</HTNum><WaferEC>80318</WaferEC><HolderType>HACARR</HolderType><Job>167187008</Job></Table>
<Table><Claimable>false</Claimable><MinorRev>71115</MinorRev><Operation>530600 ION MILL</Operation><Experiment>6794</Experiment><HTNum>162</HTNum><WaferEC>71105</WaferEC><HolderType>HACARR</HolderType><Job>16799006</Job></Table>
</Tables>
'''

import xml.etree.ElementTree as ET
tree = ET.ElementTree(ET.fromstring(xmlData))
root = tree.getroot()
print root
xmlData=''
假80601530600离子磨坊16280318HACARR167187008
假71115530600离子磨坊679416271105HACARR16799006
'''
将xml.etree.ElementTree作为ET导入
tree=ET.ElementTree(ET.fromstring(xmlData))
root=tree.getroot()
打印根

在您的例子中,如果我没记错的话,您有一个在python脚本中调用xmlData的“变量”。我想做的是直接从shell输出中读取xml——我将发送命令,用subprocess@user2907822从描述中,您正在剥离前3行并将其保存到文件中,对吗?不,我已使用telnet ip 8881 | tee namefile.xml保存了输出,并手动删除了前3行
xmlData = '''<Tables>
<Table><Claimable>false</Claimable><MinorRev>80601</MinorRev><Operation>530600 ION MILL</Operation><HTNum>162</HTNum><WaferEC>80318</WaferEC><HolderType>HACARR</HolderType><Job>167187008</Job></Table>
<Table><Claimable>false</Claimable><MinorRev>71115</MinorRev><Operation>530600 ION MILL</Operation><Experiment>6794</Experiment><HTNum>162</HTNum><WaferEC>71105</WaferEC><HolderType>HACARR</HolderType><Job>16799006</Job></Table>
</Tables>
'''

import xml.etree.ElementTree as ET
tree = ET.ElementTree(ET.fromstring(xmlData))
root = tree.getroot()
print root