使用Python删除IP地址中的前导零

时间:2021-04-23 16:50:48

Remove unnecessary zeros in IP address:

删除IP地址中不必要的零:

100.020.003.400  ->  100.20.3.400
001.200.000.004  ->  1.200.0.4
000.002.300.000  ->  0.2.300.0   (optional silly test)

My attempt does not work well in all cases:

我的尝试在所有情况下都不能很好地运作:

import re
ip = re.sub('[.]0+', '.', ip_with_zeroes)

There are similar question but for other languages:

有类似的问题,但对于其他语言:

Please provide solutions for both Python v2 and v3.

请提供Python v2和v3的解决方案。

5 个解决方案

#1


5  

Use the netaddr library and then it has the ZEROFILL flag:

使用netaddr库,然后它有ZEROFILL标志:

import netaddr

ip = "055.023.156.008"

no_zero_ip = netaddr.IPAddress(ip, flags=netaddr.ZEROFILL).ipv4()

# IPAddress('55.23.156.8')

You probably want to change the no_zero_ip result to a string or something else if you don't want the IPAddress type

如果您不想要IPAddress类型,您可能希望将no_zero_ip结果更改为字符串或其他内容

#2


5  

ip = "100.020.003.400"
print '.'.join(str(int(part)) for part in ip.split('.'))
# 100.20.3.400

#3


2  

For prior to 2.6 you can use the string-modulo operator. But let's not talk about that.

对于2.6之前的版本,您可以使用string-modulo运算符。但是我们不要谈论这个。

This should do it as far back as the format method was introduced (2.6):

这应该在引入格式方法时做到(2.6):

'.'.join('{0}'.format(int(i)) for i in ip.split('.'))

Optionally eliminate the index the for python ≥3.3 or ≥2.7 (I think):

可选择消除python≥3.3或≥2.7的索引(我认为):

'.'.join('{}'.format(int(i)) for i in ip.split('.'))

And for python ≥3.6 only, we get to f-string it up:

而对于python≥3.6,我们得到f-string up:

'.'.join(f'{int(i)}' for i in ip.split('.'))

If you can use the last, I highly recommend it. It's quite satisfying.

如果你可以使用最后一个,我强烈推荐它。这非常令人满意。

#4


0  

Use a capture group to match the last digit and copy it, to prevent all the digits from being replaced.

使用捕获组匹配最后一个数字并复制它,以防止替换所有数字。

ip = re.sub(r'\b0+(\d)', r'\1', ip_with_zeroes)

#5


0  

You can split by "." convert to integer and then string, and join by "."

你可以用“。”拆分。转换为整数,然后转换为字符串,并通过“。”连接。

ip = ".".join([str(int(i)) for i in ip.split(".")])

#1


5  

Use the netaddr library and then it has the ZEROFILL flag:

使用netaddr库,然后它有ZEROFILL标志:

import netaddr

ip = "055.023.156.008"

no_zero_ip = netaddr.IPAddress(ip, flags=netaddr.ZEROFILL).ipv4()

# IPAddress('55.23.156.8')

You probably want to change the no_zero_ip result to a string or something else if you don't want the IPAddress type

如果您不想要IPAddress类型,您可能希望将no_zero_ip结果更改为字符串或其他内容

#2


5  

ip = "100.020.003.400"
print '.'.join(str(int(part)) for part in ip.split('.'))
# 100.20.3.400

#3


2  

For prior to 2.6 you can use the string-modulo operator. But let's not talk about that.

对于2.6之前的版本,您可以使用string-modulo运算符。但是我们不要谈论这个。

This should do it as far back as the format method was introduced (2.6):

这应该在引入格式方法时做到(2.6):

'.'.join('{0}'.format(int(i)) for i in ip.split('.'))

Optionally eliminate the index the for python ≥3.3 or ≥2.7 (I think):

可选择消除python≥3.3或≥2.7的索引(我认为):

'.'.join('{}'.format(int(i)) for i in ip.split('.'))

And for python ≥3.6 only, we get to f-string it up:

而对于python≥3.6,我们得到f-string up:

'.'.join(f'{int(i)}' for i in ip.split('.'))

If you can use the last, I highly recommend it. It's quite satisfying.

如果你可以使用最后一个,我强烈推荐它。这非常令人满意。

#4


0  

Use a capture group to match the last digit and copy it, to prevent all the digits from being replaced.

使用捕获组匹配最后一个数字并复制它,以防止替换所有数字。

ip = re.sub(r'\b0+(\d)', r'\1', ip_with_zeroes)

#5


0  

You can split by "." convert to integer and then string, and join by "."

你可以用“。”拆分。转换为整数,然后转换为字符串,并通过“。”连接。

ip = ".".join([str(int(i)) for i in ip.split(".")])