def replace_in_file(path, replacements):
"""
This helper function performs a line replacement in the file located at 'path'.
:param path: path to the file to be altered
:param replacements: list of string pairs formatted as [old_line_pattern, new_line_replacement]
"""
tmp = path + '.tmp'
if isinstance(replacements[0], string_types):
replacements = [replacements]
regexs = []
for replacement in replacements:
try:
regex = re.compile(replacement[0])
except re.error:
regex = None
regexs.append(regex)
with open(tmp, 'w+') as nf:
with open(path) as of:
for line in of.readlines():
skip = False
for replacement, regex in zip(replacements, regexs):
# try a simple string match
if replacement[0] in line:
if replacement[1] in (None, ''):
skip = True
else:
line = line.replace(replacement[0], replacement[1])
break
# then try a regex match
else:
if regex is not None:
match = regex.search(line)
if match is not None:
if replacement[1] in (None, ''):
skip = True
try:
line = line.replace(match.groups(0)[0], replacement[1])
except IndexError:
line = line.replace(match.group(), replacement[1])
break
if not skip:
nf.write(line)
sh.rm(path)
sh.mv(tmp, path)
# **************************************** JSON-RELATED HELPER *****************************************
评论列表
文章目录