I have a function as follows (I am working on python2.7):
def ip_split(ip):
rtr, mask = cidr.split('/')
ip = ipaddress.ip_address(unicode(rtr))
if ip.version == 4:
n = ipaddress.IPv4Network(unicode(cidr), strict=False)
else:
n = ipaddress.IPv6Network(unicode(cidr), strict=False)
first, last = n[2], n[-2]
return n, first, last, n[1], n.network_address, mask
n_sub = "%s/%s" % ip_split('10.10.128.1/18')[3:]
Traceback (most recent call last):
File "python", line 18, in <module>
TypeError: not all arguments converted during string formatting
I am getting the typeError: not all arguments convereted during string formatting.
What can be wrong over here??
Your ip_split
function returns a 6-tuple. If you use the [3:]
slice operator, you will obtain everything from index 3
and further, so that will be a 3-tuple.
But your formatting string '%s/%s'
has only two %s
s, so Python gets confused. You can (probably) fix it with slicing from index four, so [4:]
.
A second problem is that %
takes precedence over indexing, so it sees the expression as ('%s/%s' % ip_split('10.10.128.1/18'))[3:]
, whereas I think you want to first get the last two items of the tuple. The fact that you uses spaces here does not makes a difference (yes, for scoping, Python takes spaces into account, but not for operator precedence).
Alternatively, you can use [-2:]
, which is closer to "the last two items". So:
n_sub = "%s/%s" % (ip_split('10.10.128.1/18')[-2:])