找到了如何通过回调函数对正则表达式引用进行计算,如以下问题所示:Python re.sub question
所以这是对我有用的python:
例子:
import re
test = '"Properties" : { "Col_Row": "1 - 145", ... "Col_Row": "130 - 240" ... }}'
def repl(m):
num = "%d%d" % (int(m.group(1))+1000,int(m.group(2))+1000)
string = '"Geo_ID": "%s", "Col_Row": "%s - %s",' % (num,m.group(1),m.group(2))
return string
output = re.sub(r'"Col_Row": "(.*?) - (.*?)",', repl, test)
输出:'"Properties" : { "Geo_ID": "10011145", "Col_Row": "1 - 145", ... "Geo_ID": "11301240", "Col_Row": "130 - 240" ... }}'
现在是真实的(操作文件):
input = open('fishnet.json','r')
input_list = input.readlines()
output = open('updated.json','w')
for line in input_list:
updated = re.sub(r'"Col_Row": "(.*?) - (.*?)",', repl, line)
output.write(updated)
output.close()
input.close()