python 文件操作删除某行的实例

yipeiwu_com5年前Python基础

使用continue跳过本次写循环就可以了

#文本内容
Yesterday when I was young
昨日当我年少轻狂
The tasting of life was sweet
生命的滋味是甜的
As rain upon my tongue
tasting
I lived by night and shunned the naked light of day
tasting123
And only now I see how the time ran away
tasting
 
tasting

将文本中的 tasting123删除 

with open("fileread.txt","r",encoding="utf-8") as f:
 lines = f.readlines()
 #print(lines)
with open("fileread.txt","w",encoding="utf-8") as f_w:
 for line in lines:
  if "tasting123" in line:
   continue
  f_w.write(line)

以上这篇python 文件操作删除某行的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

对python中的控制条件、循环和跳出详解

对python中的控制条件、循环和跳出详解 代码缩进(代码块): python用缩进表示代码块,没有其他语言的大括号 缩进是强制检查,整个代码缩进必须一致,否则无法运行 用2、4个空格或...

python scipy求解非线性方程的方法(fsolve/root)

python scipy求解非线性方程的方法(fsolve/root)

使用scipy.optimize模块的root和fsolve函数进行数值求解线性及非线性方程,下面直接贴上代码,代码很简单 from scipy.integrate import o...

Python3 main函数使用sys.argv传入多个参数的实现

在运维过程中,有些时候需要向main函数中传递参数,以方便运维与测试,那么怎么向main函数中传入多个参数呢,下面以python3中的main函数为例,简单讲一下。 首先我们需要impo...

Python实现的远程登录windows系统功能示例

本文实例讲述了Python实现的远程登录windows系统功能。分享给大家供大家参考,具体如下: 首先安装wmi 命令: pip install wmi 然后会报错缺少pyw...

Python在Console下显示文本进度条的方法

进度条实现原理 进度条和一般的print区别在哪里呢? 答案就是print会输出一个\n,也就是换行符,这样光标移动到了下一行行首,接着输出,之前已经通过stdout输出的东西依旧保留,...