Python / LXML / FindAll-无法将元素打印为字符串(Python新手)

时间:2018-10-17 20:46:39

标签: python lxml findall

当我在代码中也用“ >>>”标记的“ print”行中使用“ findall”时,列表中的对象将显示为XML元素。 “ .text”函数似乎没有任何作用,无论我将其放在行中的什么位置。这些元素都是IPv4或IPv6地址-不确定是否重要。大多数解决方案与我正在做的事情不同,而且我和我的同事感到沮丧。从字面上看,这是我的第一个Python脚本。

输入样本(缩短)

<rpc-reply xmlns:junos="http://xml.juniper.net/junos/18.2R1/junos">
    <isis-database-information xmlns="http://xml.juniper.net/junos/18.2R1/junos-routing" junos:style="extensive">
        <isis-database>
            <level>1</level>
            <isis-database-entry>

                -----OUTPUT OMITTED----

                <isis-prefix xmlns="http://xml.juniper.net/junos/18.2R1/junos-routing" junos:style="normal">
                    <protocol-name>IP</protocol-name>
                    <isis-topology-id>IPV4 Unicast</isis-topology-id>
Trying to print >>> <address-prefix>1.7.11.0/32</address-prefix>
                    <metric>16</metric>
                    <prefix-flag>Internal</prefix-flag>
                    <prefix-status>up</prefix-status>
                </isis-prefix>
                <isis-prefix xmlns="http://xml.juniper.net/junos/18.2R1/junos-routing" junos:style="normal">
                    <protocol-name>IP</protocol-name>
                    <isis-topology-id>IPV4 Unicast</isis-topology-id>
                    <address-prefix>1.11.12.0/24</address-prefix>
                    <metric>32</metric>
                    <prefix-flag>Internal</prefix-flag>
                    <prefix-status>up</prefix-status>
                </isis-prefix>

输出样本(缩短)

##################################################
##################################################
LSP ID: D17-HYBC03.00-00
IP Address:     [<Element address-prefix at 0x7f4cdd4d0710>, <Element address-prefix at 0x7f4cdd4d0a70>]
IPv4 Node-SID:  None
IPv6 Node-SID:  None
First SRGB Label:       None
Last SRGB Label:        None
##################################################
##################################################
LSP ID: D18-HYBC04.00-00
IP Address:     [<Element address-prefix at 0x7f4cdd4d0710>, <Element address-prefix at 0x7f4cdd4d0998>]
IPv4 Node-SID:  2018
IPv6 Node-SID:  None
First SRGB Label:       16000
Last SRGB Label:        23999
##################################################
##################################################
LSP ID: D07-DSRJ01.00-00
IP Address:     [<Element address-prefix at 0x7f4cdd4d0a28>, <Element address-prefix at 0x7f4cdd4d0908>, <Element address-prefix at 0x7f4cdd4d0710>, <Element address-prefix at 0x7f4cdd4d09e0>]
IPv4 Node-SID:  7
IPv6 Node-SID:  1007
First SRGB Label:       16000
Last SRGB Label:        23999
##################################################
##################################################
LSP ID: D08-DSRJ02.00-00
IP Address:     [<Element address-prefix at 0x7f4cdd4d0998>, <Element address-prefix at 0x7f4cdd4d0878>, <Element address-prefix at 0x7f4cdd4d0908>, <Element address-prefix at 0x7f4cdd4d0638>]
IPv4 Node-SID:  8
IPv6 Node-SID:  1008
First SRGB Label:       16000
Last SRGB Label:        23999

代码

from jnpr.junos import Device
from lxml import etree

####################
###etree.dump(DB)###
####################

if __name__ == '__main__':
    NODE = Device(host='x.x.x.x', user='root',  password='test').open()
    DB = NODE.rpc.get_isis_database_information(level='1', extensive = True, normalize = True, )
    DB_ROOT = DB.findall('.//isis-database/isis-database-entry')

    for DB_ENTRY in DB_ROOT:

        LSPID = 'lsp-id'
        PREFIX = 'isis-prefix'
        PREFIX_ADDRESS = 'address-prefix'
        SID_V4 = 'isis-node-segment/isis-node-segment-ipv4-index'
        SID_V6 = 'isis-node-segment/isis-node-segment-ipv6-index'
        LABEL_FIRST = 'isis-node-segment/isis-node-segment-block/isis-node-segment-block-first-label'
        LABEL_LAST = 'isis-node-segment/isis-node-segment-block/isis-node-segment-block-last-label'

        print('#' * 50) 
        print('#' * 50)
        print("LSP ID:\t{LSPID}".format(LSPID=DB_ENTRY.findtext(LSPID)))
    >>> print("IP Address:\t{PREFIX_ADDRESS}".format(PREFIX_ADDRESS=DB_ENTRY.findall("isis-prefix/[metric='0']address-prefix")))    
        print("IPv4 Node-SID:\t{SID_V4}".format(SID_V4=DB_ENTRY.findtext(SID_V4)))  
        print("IPv6 Node-SID:\t{SID_V6}".format(SID_V6=DB_ENTRY.findtext(SID_V6)))      
        print("First SRGB Label:\t{LABEL_FIRST}".format(LABEL_FIRST=DB_ENTRY.findtext(LABEL_FIRST)))        
        print("Last SRGB Label:\t{LABEL_LAST}".format(LABEL_LAST=DB_ENTRY.findtext(LABEL_LAST)))

1 个答案:

答案 0 :(得分:0)

如果DB_ENTRY<isis-database-entry>节点,则期望DB_ENTRY.findall("isis-prefix/address-prefix")找到所有(2)个<address-prefix>标签的列表。

是的,这就是输出样本中显示的内容:IP Address: [<Element address-prefix at 0x7f4cdd4d0710>,...]

似乎您真的想要两个循环,如下所示:

for DB_ENTRY in list(DB_ROOT):
    for ISIS_PREFIX in DB_ENTRY:
        print("IP Address:\t{PREFIX_ADDRESS}".format(PREFIX_ADDRESS=ISIS_PREFIX.findall("address-prefix")[0].text))

已更改为DB_ENTRY.findall(),而不是ISIS_PREFIX.findall()。并且该.findall()返回一个列表,因此我在末尾添加了[0]以使IP地址节点脱离列表,然后添加了.text以返回实际的IP地址而不是XML节点信息。

我的测试显示了以下输出:

IP Address: 1.7.11.0/32
IP Address: 1.11.12.0/24