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.
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', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
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)
error message
AttributeError: 'float' object has no attribute 'partition'
Previous link which works for IP addresses
Pair IP addresses in a list pulled from xlrd
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])
However, the output ends up being
[['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'], []]
Which is not what you need, beacuse it has ['Receive']
and []
, which are not valid pool ports.
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']]