Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何在 Python 中替换文件中的多个值(字符串)而不将其写入新文件?
我只找到了如何通过实际将其写入新文件来替换它的方法,或者如果不是这种情况,它只适用于一个项目,但是我需要替换多个项目!
输入是应该被替换的字符串以及它应该被替换的字符串。输入是正确的,不需要检查它是否真的在文件中。
你可能之后r+允许阅读和写作。
r+
with open("testfile.txt","r+") as fileTest: rawLines = fileTest.readlines() # Do replacement 1 # Do replacement 2 for line in rawLines: fileTest.write(line+"\n")
有任何问题,请告诉我。