Python:如何给文件中的每个整数加1

Python: how to add 1 to every integer in a file

我有一个脚本,其中包含与字典中的匹配键相关的索引。最近的一项更改要求我将所有数字都移到 1 以上(每个数字加 1)。例如,如果文件包含以下内容:

form['formA'] = {
  'title': 'titleA',
  'number': 3,
  'numbers': [1,2,4,5]
}

form['formB'] = {
  'title': 'titleB',
  'number': 7,
  'numbers': [8,9,10,11]
}

我希望每个整数都大一。所以它会变成:

form['formA'] = {
  'title': 'titleA',
  'number': 4,
  'numbers': [2,3,5,6]
}

form['formB'] = {
  'title': 'titleB',
  'number': 8,
  'numbers': [9,10,11,12]
}

在所有类型错误、属性错误和格式错误之间,我不知道该怎么做。这可能是我最接近的尝试:

#read from the file
f = open(currdir, 'r')
content = f.readlines()
f.close()

addbrackets = False    #is it a list
for line in content:
    if "form" not in line:
        #grab only the values to the right of the colon
        rightside = line.split(":")[-1]
        list_of_nums = rightside

        #remove brackets
        if "[" in rightside:
            addbrackets = True
            removebrackets = rightside.replace("[","").replace("]","")
            list_of_nums = removebrackets.split(",")

        #search for all integers in the list and add 1
        for num in list_of_nums:
            if type(num) is int:
                num += 1
                numindex = list_of_nums.index(num)
                list_of_nums[numindex] = num

        #plug new values into content
        lineindex = content.index(line)
        if addbrackets:
            content[lineindex] = line.replace(rightside, "[" + ",".join(list_of_nums))[:-1] + "],"
            addbrackets = False
        else:
            content[lineindex] = line.replace(rightside, "".join(list_of_nums))

#write to the new file
f = open(newdir, 'w')
f.write("".join(content))
f.close()

但是,这只是设法弄乱了格式。有什么办法吗?

谢谢。

如果您真的想保留格式并且希望对重新格式化的内容不加区别(例如,所有由单词边界分隔的整数),那么这是一个简单的正则表达式 search/replace,您需要搜索词边界 (\b)、任意数量的连续整数 (\d+),然后是终止词边界 (\b)。这将增加 'foo 15 bar''[1]''[1, 2]' 等字符串中的数字,但不会增加 'foo15bar''foo15':

import re
with open(yourfilename) as fin:
  s = fin.read()
print re.sub(r'\b\d+\b', lambda m: str(int(m.group())+1), s)

如果我将您的数据作为字符串分配给 s 最后一行 运行,我得到:

form['formA'] = {
  'title' = 'titleA',
  'number' = 4,
  'numbers' = [2,3,5,6]
}

form['formB'] = {
  'title' = 'titleB',
  'number' = 8,
  'numbers' = [9,10,11,12]
}

这似乎是你想要的。当然,如果您有一些不想增加的数字,那么这将不起作用——您需要一种更智能的方法来解析文件。

使用正则表达式:

foo="""
form['formA'] = {
  'title' = 'titleA',
  'number' = 3,
  'numbers' = [1,2,4,5]
}

form['formB'] = {
  'title' = 'titleB',
  'number' = 7,
  'numbers' = [8,9,10,11]
}
"""

def incNumbers(s):
  def inc(m):
    return str(int(m.group())+1)
  return re.sub(r'(\d+)', inc, s)


def go(m):
  return m.group(1) + incNumbers(m.group(2))

r = re.compile('^( *\'numbers?\' =)(.*)', re.MULTILINE)
print re.sub(r, go, foo)