1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import re
def updateDictValue(fileName, key, value): fileLineContent = []
with open(fileName, 'r') as f: line = f.readline() while line: regex = key + '\s*=\s*"(.*)"' pattern = re.compile(regex) m = pattern.search(line) if m: print(m.group(1)) fileLineContent.append(line.replace(m.group(1), value)) else: fileLineContent.append(line) line = f.readline()
with open(fileName, 'w') as f: f.writelines(fileLineContent)
|