📌  相关文章
📜  替换文件中文本的Python程序

📅  最后修改于: 2022-05-13 01:54:52.490000             🧑  作者: Mango

替换文件中文本的Python程序

在本文中,我们将使用Python替换文件中的文本。替换文本可以是擦除文件的整个内容并用新文本替换它,也可以意味着仅修改现有文本中的特定单词或句子。

方法 1 :删除所有文本并在同一文件中写入新文本

在此方法中,我们替换存储在文本文件中的所有文本,为此,我们将以读写模式打开文件并重写所有文本。

Python3
# Python program to replace text in a file
s = input("Enter text to replace the existing contents:")
f = open("file.txt", "r+")
  
# file.txt is an example here,
# it should be replaced with the file name
# r+ mode opens the file in read and write mode
f.truncate(0)
f.write(s)
f.close()
print("Text successfully replaced")


Python3
# Python program to replace text in a file
x = input("enter text to be replaced:")
y = input("enter text that will replace:")
  
# file.txt should be replaced with
# the actual text file name
f = open("file.txt", "r+")
  
# each sentence becomes an element in the list l
l = f.readlines()
  
# acts as a counter to know the
# index of the element to be replaced
c = 0
for i in l:
    if x in i:
  
        # Replacement carries the value
        # of the text to be replaced
        Replacement = i.replace(x, y)
  
        # chsnges are made in the list
        l = Replacement
    c += 1
  
# The pre existing text in the file is erased
f.truncate(0)
  
# the modified list is written into
# the file thereby replacing the old text
f.writelines(l)
f.close()
print("Text succesfully replaced")


Python
# Program to replace text in a file
import os
x = input("Enter text that will replace the existing text:")
f = open("file.txt", "r+")
f1 = open("new.txt", "r+")
  
f1.write(x)
os.remove("file.txt")
os.rename("new.txt", "file.txt")
f1.close()
  
print("File replaced")


Python3
import sys
import fileinput
  
x = input("Enter text to be replaced:")
y = input("Enter replacement text")
  
for l in fileinput.input(files = "file.txt"):
    l = l.replace(x, y)
    sys.stdout.write(l)


输出:

Enter text to replace the existing contents: Geeks
Text successfully replaced

方法二:在for循环中使用Replace函数

简单的 for 循环是遍历给定文本文件中的每一行并找到我们要替换的行的常规方法。然后,可以使用 replace()函数替换所需的行。最后以写入方式打开文件,将替换的内容写入给定文件中。



蟒蛇3

# Python program to replace text in a file
x = input("enter text to be replaced:")
y = input("enter text that will replace:")
  
# file.txt should be replaced with
# the actual text file name
f = open("file.txt", "r+")
  
# each sentence becomes an element in the list l
l = f.readlines()
  
# acts as a counter to know the
# index of the element to be replaced
c = 0
for i in l:
    if x in i:
  
        # Replacement carries the value
        # of the text to be replaced
        Replacement = i.replace(x, y)
  
        # chsnges are made in the list
        l = Replacement
    c += 1
  
# The pre existing text in the file is erased
f.truncate(0)
  
# the modified list is written into
# the file thereby replacing the old text
f.writelines(l)
f.close()
print("Text succesfully replaced")

输出:

Enter text to be replaced: Geeks
Enter text that will replace: Geekforgeeks
Text successfully replaced

方法 3 :使用 OS 模块将文件替换为新文本

我们使用 os 模块用原始文件名重命名新文件。在这种方法中,我们不是编辑已经存在的文件,而是使用修改后的内容创建一个新文件,然后删除旧文件并重命名新文件。

Python

# Program to replace text in a file
import os
x = input("Enter text that will replace the existing text:")
f = open("file.txt", "r+")
f1 = open("new.txt", "r+")
  
f1.write(x)
os.remove("file.txt")
os.rename("new.txt", "file.txt")
f1.close()
  
print("File replaced")

输出:

Enter text that will replace the existing text: geeks
File replaced

方法四:使用fileinput.input()

fileinput.input() 方法逐行获取文件作为输入,主要用于追加和更新给定文件中的数据。需要将 fileinput 和 sys 模块导入到当前的Python代码中,才能正确运行代码。以下代码使用 fileinput.input()函数替换Python中一行中的文本。

蟒蛇3

import sys
import fileinput
  
x = input("Enter text to be replaced:")
y = input("Enter replacement text")
  
for l in fileinput.input(files = "file.txt"):
    l = l.replace(x, y)
    sys.stdout.write(l)

输出:

Enter text to be replaced: Geeks
Enter replacement text: Geeksforgeeks