我有这样的代码:为什么这段代码不会替换这段文字?
count = -1
with open("text.txt", "r") as f:
content = f.readlines()
for line in content:
if line.startswith(" <Vertex>"):
count += 1
line = line.replace(str(line), str(" <Vertex> " + str(count) + " {\n"))
continue
else:
pass
with open("text2.txt", "w") as f:
f.writelines(content)
当它运行时,就应更换,与" <Vertex>"
始于" <Vertex> 0 {"
,或任何数量的计数上的任何线。
当我运行它时,它运行良好,但是当我打开新的text2.txt
文件时,它与text.txt
完全相同。
我在做什么错?
相关:[如何使用Python搜索和替换文件中的文本?](http ://stackoverflow.com/q/17140886/4279) – jfs