Py学习  »  Python

python-从ip地址中删除前导零

Arif • 5 年前 • 1364 次点击  

我试图从IP地址中删除前导零,但它不起作用

import re

def remove_zeros_from_ip(ip_add):
  string = re.sub('(?!(\.0\.))(\.[0]{1,2})', '.', ip_add)  
  return string

ip = '10.0.01.10'
print(remove_zeros_from_ip(ip))

ip1 = '10.00.002.20'
print(remove_zeros_from_ip(ip1))

但是得到这个-

10.0.1.10
10..2.20

如何获得?-

10.0.1.10
10.0.2.20
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43170
 
1364 次点击  
文章 [ 3 ]  |  最新文章 5 年前