python - 在 Python 中递增版本号

标签 python string int

我正在尝试对 CVS 中的文件进行版本号更新。

我最初的逻辑是更新一个 float (1.1 --> 1.2 --> 1.3),在我达到 1.9 之前它工作正常,然后它更新到 2.0。

我正在尝试使用此逻辑更新到 1.10,但是当我尝试在 1.x (ver[1] += 1) 中增加 x 时它会抛出错误。

def replace_string():
    with open(filename) as f:
        found = False
    #for line in fileinput.input(filename, inplace=1):
        for line in f:
            if re.search("CVS Header", line):
                print 'Old Line \n' + line

                ####################################################################################
                #  Below logic:                                                                    #
                #  if length of revision number is 4 characters (e.g. 1.15) then increment by 0.01 #
                #  else if it is 3 characters (e.g. 1.5) then increment by 0.1                     #
                ####################################################################################

                if len(line.split("$Revision: ")[1].split()[0]) == 4:
                    ver = line.split("$Revision: ")[1].split()[0]
                    ver = [int(x) for x in ver.split('.')]
                    ver = '{0[0]}.{0[1]}'.format(ver)
                    ver[1] += 1
                    print ver
                    new_line = str.replace(line, line.split("$Revision: ")[1].split()[0], ver)
                    print new_line

                elif len(line.split("$Revision: ")[1].split()[0]) == 3:
                    ver = line.split("$Revision: ")[1].split()[0]
                    ver = [int(x) for x in ver.split('.')]
                    ver = '{0[0]}.{0[1]}'.format(ver)
                    ver[1] += 1
                    print ver
                    new_line = str.replace(line, line.split("$Revision: ")[1].split()[0], ver)
            ###
                newer_line = str.replace(new_line, line.split("$Author: ")[1].split()[0], username)
                newest_line = str.replace(newer_line, line.split("$Date: ")[1].split()[0], today)
                current_line = str.replace(newest_line, line.split("$Date: ")[1].split()[1], time)
                found = True
                print 'New Line \n' + current_line

    if not found:
        print "No CVS Header exists in %s" % filename  

if __name__ == "__main__":
    #args = parser.parse_args()
    replace_string()

最佳答案

你可以使用:

ver, rev = str(1.9).split('.')
ver + '.' + str(int(rev)+1)  # result: '1.10'

有了这个,只要输入始终是一个字符串,您就可以摆脱对存在的数字数量的检查。在我的示例中它不是,这就是为什么我将 1.9 转换为字符串,但如果用于 float 1.10 而不是字符串 '1.10'。但你不必担心,因为你的输入

line.split("$Revision: ")[1].split()[0]

已经是字符串了。

关于python - 在 Python 中递增版本号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26868137/

相关文章:

python - 如何使用 subprocesses.call 并将一个输出通过管道传输到 txt 文件?

python - 如何从我单击的点单击并拖动图像,而不是在 python 的中心

javascript - 正则表达式来定位一组带有包含和排除的单词的可选列表的单词

Java - 公共(public)字符串(字符 [] 值)

python - 在Python中等待下载完成

java - 如果我们连接,System.out.println() 会创建一个字符串吗?

c - 将一个int值插入到一个char类型的指针中

python - 从字典中使用replace()时出现类型错误

c - 此代码中百分比字符的含义是什么?

python - 在本地网络上运行 Bokeh 服务器