Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何删除“;领先的;版本号字符串中的零?_Python_Python 2.7 - Fatal编程技术网

Python 如何删除“;领先的;版本号字符串中的零?

Python 如何删除“;领先的;版本号字符串中的零?,python,python-2.7,Python,Python 2.7,如何从以下版本号字符串中删除“前导”零 5.002.008.80911-->5.2.8.80911 Thx.在以下位置使用实用程序功能: 您可以将re.sub与反向查找正则表达式模式一起使用: re.sub(r'(?<!\d)0+', '', '5.002.008.80911') 使用str.split和str.join Ex: s = "5.002.008.80911" print(".".join(str(int(i)) for i in s.split("."))) 5.2.8.

如何从以下版本号字符串中删除“前导”零

5.002.008.80911-->5.2.8.80911

Thx.

在以下位置使用实用程序功能:


您可以将
re.sub
与反向查找正则表达式模式一起使用:

re.sub(r'(?<!\d)0+', '', '5.002.008.80911')

使用
str.split
str.join

Ex:

s = "5.002.008.80911"
print(".".join(str(int(i)) for i in s.split(".")))
5.2.8.80911
输出:

s = "5.002.008.80911"
print(".".join(str(int(i)) for i in s.split(".")))
5.2.8.80911
那么:

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

return res
基本上是按点拆分,然后将字符串转换为整数,去掉零,然后再转换回字符串