Python:点分离版本比较



我正在编写一个脚本,我需要在其中比较二进制文件的版本,因此例如我需要获得大于 10.1.30 的 10.2.3。如果我删除点,我会得到 1023 和 10130,这会颠倒我的比较。

10.2.3 > 10.1.30  ==  1023 !> 10130 

到目前为止,我出来的唯一解决方案是:

1. do split(".") on version number 
2. for each elemet of list i got, check  if len() is eq 1 , add one zero from  the left.
3. glue all  elements together and get  int (10.1.30 will became 100130).
4. process second version number same way and compare both as regular ints.

这边:

10.2.3 > 10.1.30  ==  100203 > 100130 

这是比较版本的唯一方法还是其他方法?

您可以借用distutils及其后续人员用于比较版本号的代码

from distutils.version import StrictVersion # Or LooseVersion, if you prefer
if StrictVersion('10.2.3') > StrictVersion('10.2'):
    print "10.2.3 is newer"

你可以试试:

list(map(int, '10.2.3'.split('.'))) > list(map(int, '10.1.30'.split('.')))
Out[216]: True

最新更新