我有一个脚本,其中包含与字典中匹配键有关的索引。最近的更改要求我将所有数字移到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()
然而,这只是设法弄乱格式化。有没有办法做到这一点?
感谢。
答案 0 :(得分:4)
如果你真的想保留格式并且想要不加思索你重新格式化的内容(例如,所有由字边界分隔的整数),那么这是一个简单的正则表达式搜索/替换,你想要搜索字边界(\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]
}
这似乎是你想要的。当然,如果你有一些你想要增加的数字,那么这不会起作用 - 你需要一种更聪明的方法来解析文件。
答案 1 :(得分:2)
使用正则表达式:
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)