导入xlrd后从列表中配对端口号 [英] Pair Port Numbers from list after xlrd import

查看:82
本文介绍了导入xlrd后从列表中配对端口号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此问题上,我获得了帮助,此站点上的IP地址配对非常有用.我试图修改相同的脚本以用于配对端口号,但不断出现float属性错误.我曾尝试更改它,但作为新手,我未能取得任何进展,请提供帮助.

I had help on this matter pairing IP Addresses from this site which worked. I tried to amend the same script to use to pair Port numbers but keep getting a float attribute error. I have tried changing it but as a newbie I have failed to make any headway please help.

这是数据 ['','','','','','','','池成员端口','',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,'','','','','','','','','' ,'','','接收字符串','','','','','','','','','','','','','' ,,",,",]

This is the data ['', '', '', '', '', '', '', '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', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']

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

This is my amended script 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:浮动"对象没有属性分区"

error message AttributeError: 'float' object has no attribute 'partition'

适用于IP地址的上一个链接 列表中的对IP地址从xlrd中拉出

Previous link which works for IP addresses Pair IP addresses in a list pulled from xlrd

推荐答案

错误确实很简单.您的输入列表的端口值(例如11001.0)以浮点数存储,因此代码会中断.即时修复方法是将x强制转换为分区之前的字符串.

The error is simple, really. Your input list has it's ports values (e.g. 11001.0) stored as floats, therefore the code breaks. The instant fix would be casting x to string right before the partition.

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'],['接收'],[]]

[['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'], []]

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

Which is not what you need, beacuse it has ['Receive'] and [], which are not valid pool ports.

因此,受@cᴏʟᴅsᴘᴇᴇᴅ对上一个问题的回答的启发,我建议您使用正则表达式.

So, inspired by @cᴏʟᴅsᴘᴇᴇᴅ 's answer to the previous question, I would recommend you to use regex.

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']]

这篇关于导入xlrd后从列表中配对端口号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆