在 Python 中替换文件中的一行 您所在的位置:网站首页 python修改列表中元素只剩第一列 在 Python 中替换文件中的一行

在 Python 中替换文件中的一行

2023-08-04 06:57| 来源: 网络整理| 查看: 265

在 Python 中使用 for 循环和 replace() 函数替换文件中的一行 在 Python 中用更新的内容创建一个新文件并替换原始文件 在 Python 中使用 fileinput.input() 函数替换一行中的文本 在 Python 中使用 re 模块替换一行中的文本

文件处理被认为是任何 Web 应用程序的重要方面。与大多数编程语言类似,Python 完全能够支持文件处理。它允许你处理不同类型的文件,本质上是执行一些基本操作,例如读取和写入。它还提供了一些其他文件处理选项来操作文件。

十大代表決策的塔羅牌

Please enable JavaScript

十大代表決策的塔羅牌

本教程演示了可用于在 Python 中替换文件中的行的不同方法。

为了演示在本文中替换文件中一行的不同方法,我们将使用一个文本文件 motivation.txt。

文本文件(motivation.txt):

There are always second chances in life. Anybody can bounce back from hardships. The hardships that we face are just a test to check our will. A strong will is all it takes to bounce back from a loss.

该文件的存储目录路径可能在读者电脑上有所不同;因此,建议进行相应调整。

在 Python 中使用 for 循环和 replace() 函数替换文件中的一行

open() 函数用于在 Python 中打开文件。该文件可以以文本或二进制格式打开,这取决于程序员。open() 函数有多种模式,所有这些模式都提供了不同的可访问性选项来打开文件。

简单的 for 循环是遍历给定文本文件中的每一行并找到我们要替换的行的传统方法。然后,可以使用 replace() 函数替换所需的行。所有这些都是在阅读模式下完成的。最后,以 write 模式打开文件,并将替换的内容写入给定文件中。

以下代码使用 for 循环和 replace() 函数。

# opening the file in read mode file = open("motivation.txt", "r") replacement = "" # using the for loop for line in file: line = line.strip() changes = line.replace("hardships", "situations") replacement = replacement + changes + "\n" file.close() # opening the file in write mode fout = open("motivation.txt", "w") fout.write(replacement) fout.close() 在 Python 中用更新的内容创建一个新文件并替换原始文件

有几个函数正在工作来实现这个过程。要成功执行此方法,必须了解所有这些函数的过程。我们需要将三个模块中的几个函数导入到 Python 代码中。

首先,我们需要从 tempfile 模块导入 mkstemp() 函数。此函数用于返回一个元组作为输出以及路径和文件描述符。 然后,我们需要从 shutil 模块导入两个函数。第一个函数是 copymode() 函数,用于将权限位从源路径复制到目标路径。第二个函数是 move() 函数,它允许将文件从一个地方移动到另一个地方。 最后,我们需要从 OS 模块导入 remove() 函数。此功能允许删除路径。

所有这些模块都需要导入到当前的 Python 代码中才能运行代码而不会出现任何错误。实现这种方式的示例代码如下。

from tempfile import mkstemp from shutil import move, copymode from os import fdopen, remove # storing the path where the file is saved on your device as a variable path="C:\\Users\Admin\Desktop\python\test\motivation.txt" def replacement(filepath, hardships, situations): # Creating a temp file fd, abspath = mkstemp() with fdopen(fd,'w') as file1: with open(filepath,'r') as file0: for line in file0: file1.write(line.replace(hardships,situations)) copymode(filepath, abspath) remove(filepath) move(abspath, filepath) replacement(path,'hardships','situations') 在 Python 中使用 fileinput.input() 函数替换一行中的文本

fileinput.input() 方法逐行获取文件作为输入,主要用于追加和更新给定文件中的数据。

fileinput 和 sys 模块需要被导入到当前的 Python 代码中,以便在没有任何错误的情况下运行代码。以下代码使用 fileinput.input() 函数替换 Python 中一行中的文本。

import fileinput import sys def replacement(file, previousw, nextw): for line in fileinput.input(file, inplace=1): line = line.replace(previousw, nextw) sys.stdout.write(line) var1 = "hardships" var2 = "situations" file = "motivation.txt" replacement(file, var1, var2) 在 Python 中使用 re 模块替换一行中的文本

re 模块是 Python 提供给处理正则表达式的程序员的内置模块,需要将其导入到代码中。它有助于执行在给定的特定字符串中搜索模式的任务。

在这个方法中,我们使用了来自 RegEx 模块的两个函数,即 re.compile() 和 re.escape()。re.compile() 函数用于从正则表达式模式生成正则表达式对象,然后用于匹配。re.compile() 函数用于忽略或转义 Python 中的特殊字符。

以下代码使用 Regex 模块中的几个函数来替换 Python 中一行中的文本。

import re def replacement(Path, text, subs, flags=0): with open(filepath, "r+") as f1: contents = f1.read() pattern = re.compile(re.escape(text), flags) contents = pattern.sub(subs, contents) f1.seek(0) f1.truncate() f1.write(file_contents) filepath="motivation.txt" text="hardships" subs="situations" replacement(filepath, text, subs)

在这里,我们还使用了 sub() 函数来用字符串或指定函数的结果替换给定的模式。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有