[英]The current code I am using is not working to replace my text to other text within a .txt file, how do I replace this with Python?
Currently using this code目前使用此代码
f1 = open('C:\Users\folderName\Python Tests\data.txt', 'r')
f2 = open('C:\Users\folderName\Python Tests\data.txt', 'w')
for line in f1:
f2.write(line.replace('pyton', 'python'))
f1.close()
f2.close()
Im using Jupyter Lab and Python 3.7.5 64-bit我使用 Jupyter Lab 和 Python 3.7.5 64 位
any help?有什么帮助吗?
The issue at matter is actually for a MUCH bigger file that lives on my local computer, I have a notepad file that has about a million lines of information.问题实际上是针对我本地计算机上的一个更大的文件,我有一个包含大约一百万行信息的记事本文件。
What i really need is to replace " " with "|"我真正需要的是用“|”替换“”
But im looking to automate this, instead of going in the file and hitting CTLR + H everytime但是我希望自动执行此操作,而不是每次都进入文件并点击 CTLR + H
thanks guys谢谢你们
Here is how you can easily read and replace specific word/letter/space etc from file with another text/space.这是您如何轻松地从文件中读取和替换特定单词/字母/空格等的方法。
f1 = open("data.txt", "r")
text = f1.read();
f1.close()
f2 = open("data.txt", "w")
text = text.replace(" ","|")
f2.write(text);
f2.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.