简体   繁体   English

我正在使用的当前代码无法将我的文本替换为 .txt 文件中的其他文本,我该如何用 Python 替换它?

[英]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.

相关问题 如何使用 Python 将文本文件中仅出现一次的单词替换为其他单词? - How do you replace the words that are only appearing once with other word within a text file using Python? 如何使用 %s 替换 python 文件中的文本? - How can I use %s to replace text within a file in python? 如何替换不在当前循环中的文本文件中的行 Python - How do I replace a line in a text file thats not in the current loop Python 为什么我不能替换 .txt 文件中的文本 - Why can I not replace text in .txt file 如何使用Python替换文件中的大部分文本? - How can I replace a large portion of a text in a file using Python? 我正在使用 Python 读取文本文件,但我想删除 txt 文件中 /* 和 /* 之间的任何文本 - I am reading a text file using Python but I would like to delete any text within the txt file that comes between /* and /* 使用Python在文本文件中查找和替换 - Find and replace within a text file using Python 如何替换txt文件中已经存在的数据--python - How do I replace data that is already existed in txt file --python 我如何只在textrun中替换文本 - how do I replace text only within a textrun 如何用.sql文件替换当前正在运行的MySQL数据库? - How do I replace the current working MySQL database with a .sql file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM