[英]Finding and Replacing string in a line
我正在尝试打开一个文件并搜索一行中的特定字符串并将其替换为另一个字符串。
我正在尝试以下代码。
def myFunct(file, test, patter, replace):
with open(file, mode='r') as f:
for line in f.readline():
if str(line).__contains__(test):
if patter in line:
print("Found here\n")
print(line)
f.close()
代码似乎没有进入 for 循环。 有什么建议 ?
我也尝试过类似的解决方案来解决同样的问题。
您只阅读第一行并遍历该行的单个字符。 您必须删除readline
:
def myFunct(file, test, patter, replace):
with open(file, mode='r') as f:
for line in f:
if test in line and patter in line:
print("Found here\n")
print(line)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.