我有一个名为front.txt
:的文本文件
<a href="https://website/project_name.html" target="_blank">
<img src="https://website/project_name.webp" class="img img-responsive">
我正在尝试开发一个Python代码;项目名称"并将其替换为它们的名称。
这就是我所做的:
filename = 'front.txt'
with open(filename, 'r+') as f:
lines = f.readlines()
your_project_name = input("Project Name? ")
for line in lines:
line.replace("project_name", your_project_name)
print(line.strip())
当我运行Python程序时,它无法替换"project_name"
。
Replace不就地操作,因此必须分配值才能使其工作。
filename = 'front.txt'
with open(filename, 'r+') as f:
lines = f.readlines()
your_project_name = input("Project Name? ")
for line in lines:
line = line.replace("project_name", your_project_name)
print(line.strip())