如何获得被替换键值的行号?目前对于它的函数不同,如何合并它,使其在替换字符串时具有行号。
filedata= is a path of file. In which i need to replace strings.
old_new_dict = {'hi':'bye','old':'new'}
def replace_oc(file):
lines = file.readlines()
line_number = None
for i, line in enumerate(lines):
line_number = i + 1
break
return line_number
def replacee(path, pattern):
for key, value in old_new_dict.items():
if key in filedata:
print("there")
filedata = filedata.replace(key, value)
else:
print("not there")
1条答案
按热度按时间zzzyeukh1#
你可以把文件数据分解成行,在实际替换之前检查要替换的单词。例如:
输出: