【发布时间】:2023-02-05 01:19:08
【问题描述】:
如何获取替换键值的行号? 目前它的功能不同,我如何在替换字符串时将它组合成行号。
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")
【问题讨论】:
-
什么是
filedata?请使用filedata的定义编辑您的代码,以便代码 sn-p 变为 minimal reproducible example。 -
这段代码中有很多非常基本的错误,以至于无法运行它,并且很难单独根据代码确定意图是什么。如果你完全被卡住了并且需要帮助让代码工作,你应该写一份关于代码内容的英文描述应该这样做可以建议代码更改,使您更接近您的意图。
-
抱歉,我已经更新了问题。谢谢@Samwise
标签: python python-3.x dictionary for-loop replace