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通过urllib2获取带有中文参数url内容的方法

本文实例讲述了python通过urllib2获取带有中文参数url内容的方法。分享给大家供大家参考。具体如下: 对于中文的参数如果不进行编码的话,python的urllib2直接处理会报...

Python matplotlib绘制饼状图功能示例

Python matplotlib绘制饼状图功能示例

本文实例讲述了Python matplotlib绘制饼状图功能。分享给大家供大家参考,具体如下: 一 代码 import numpy as np import matplotlib....

python生成多个只含0,1元素的随机数组或列表的实例

如下所示: >>> import numpy as np >>> myarray= np.random.randint(0,2,10)输出只含0,...

Python实现PS滤镜功能之波浪特效示例

Python实现PS滤镜功能之波浪特效示例

本文实例讲述了Python实现PS滤镜功能之波浪特效。分享给大家供大家参考,具体如下: 这里用 Python 实现 PS 滤镜的波浪特效,具体效果可以参考附录说明 import nu...

Python ORM框架SQLAlchemy学习笔记之映射类使用实例和Session会话介绍

1. 创建映射类的实例(Instance) 前面介绍了如何将数据库实体表映射到Python类上,下面我们可以创建这个类的一个实例(Instance),我们还是以前一篇文章的User类为例...