在xlrd导入后从列表中配对端口号

时间:2017-09-20 08:44:52

标签: list slice xlrd

我帮助解决了这个网站的IP地址配对问题。我试图修改相同的脚本以用于配对端口号但仍然得到浮动属性错误。我试过改变它但作为一个新手我没有取得任何进展请帮忙。

这是数据 ['','','','','','','','Pool Member Port','',10001.0,10001.0,'','','','','', '','',11001.0,11001.0,'','','','','','','',12001.0,12001.0,'','','','','', '','',14001.0,14001.0,'','','','','','','',14001.0,14001.0,'','','','','', '','',14001.0,14001.0,'','','','','','','',14001.0,14001.0,'','','','','', '','',14001.0,'',10001.0,'','','','','','','','','11001.0,'','','',''' ,'','','','',12001.0,'','','','','','','','','14001.0,'','','', '','','','','',22.0,'','','','','','','',''',22.0,'',22.0,''' ,'','','','','','','',14001.0,'','','','','','','','',''' ,'','','接收字符串','','','','','','','','','','','','',''' ,'','','','','']

这是我修改过的脚本 Pool_Ports = [[]]

for x in PPoData[PPoData.index('Pool Member Port'):]:
    if not x:
        if Pool_Ports[-1]:
            Pool_Ports.append([])

    else:
        #Pool_Ports[-1].append(x.partition(' ')[0])


print(Pool_Ports)

错误消息 AttributeError:'float'对象没有属性'partition'

适用于IP地址的上一个链接 Pair IP addresses in a list pulled from xlrd

1 个答案:

答案 0 :(得分:1)

错误很简单,真的。您的输入列表将其端口值(例如11001.0)存储为浮点数,因此代码中断。即时修复将在分区之前将x转换为字符串。

Pool_Ports[-1].append(str(x).partition(' ')[0])

然而,输出结束

  

[['10001.0','10001.0'],['11001.0','11001.0'],['12001.0',   '12001.0'],['14001.0','14001.0'],['14001.0','14001.0'],   ['14001.0','14001.0'],['14001.0','14001.0'],['14001.0'],   ['10001.0'],['11001.0'],['12001.0'],['14001.0'],['22 .0'],   ['22 .0'],['22 .0'],['14001.0'],['接收'],[]]

这不是您所需要的,因为它有['Receive'][],它们不是有效的池端口。

所以,受@cᴏʟᴅsᴘᴇᴇᴅ对前一个问题的回答的启发,我建议你使用正则表达式。

import re

PPoData = ['', '', '', '', '', '', '', 'Pool Member Port', '', 10001.0, 10001.0, '', '', '', '', '', '', '', 11001.0, 11001.0, '', '', '', '', '', '', '', 12001.0, 12001.0, '', '', '', '', '', '', '', 14001.0, 14001.0, '', '', '', '', '', '', '', 14001.0, 14001.0, '', '', '', '', '', '', '', 14001.0, 14001.0, '', '', '', '', '', '', '', 14001.0, 14001.0, '', '', '', '', '', '', '', 14001.0, '', 10001.0, '', '', '', '', '', '', '', '', 11001.0, '', '', '', '', '', '', '', '', 12001.0, '', '', '', '', '', '', '', '', 14001.0, '', '', '', '', '', '', '', '', 22.0, '', '', '', '', '', '', '', '', 22.0, '', 22.0, '', '', '', '', '', '', '', '', 14001.0, '', '', '', '', '', '', '', '', '', '', '', 'Receive string', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']

pattern = '\d+\.\d+'

Pool_Ports = [[]]
for x in PPoData:
    if not x and Pool_Ports[-1]:
        Pool_Ports.append([])

    m = re.match(pattern, str(x))
    if m:
        Pool_Ports[-1].append(str(x))

if [] in Pool_Ports:
    Pool_Ports.remove([])
print(Pool_Ports)
# [['10001.0', '10001.0'], ['11001.0', '11001.0'], ['12001.0', '12001.0'], ['14001.0', '14001.0'], ['14001.0', '14001.0'], ['14001.0', '14001.0'], ['14001.0', '14001.0'], ['14001.0'], ['10001.0'], ['11001.0'], ['12001.0'], ['14001.0'], ['22.0'], ['22.0'], ['22.0'], ['14001.0']]