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

yipeiwu_com6年前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设计】。

相关文章

Python3用tkinter和PIL实现看图工具

Python3用tkinter和PIL实现看图工具

需求 想做看图工具的,必然要支持jpg、png等常见格式,但tkinter是个纯粹的GUI库,不像GTK、QT那样大而全,所以只支持gif和ppm两种格式,局限很大,必须搭配图像处理库,...

Django模板Templates使用方法详解

Django模板Templates使用方法详解

一、django的模板: 在settings.py的文件中可以看到并设置这个模板。 1.直接映射: 通过建立的文件夹(templates)和文件(html)来映射。 <!D...

Python实现读取机器硬件信息的方法示例

本文实例讲述了Python实现读取机器硬件信息的方法。分享给大家供大家参考,具体如下: 本人最近新学python ,用到关于机器的相关信息,经过一番研究,从网上查找资料,经过测试,总结了...

python多个模块py文件的数据共享实例

模块a.py 想用 b.py中公有数据 cnt b的python文件 #!/usr/bin/env python # coding:utf8 from wx import Cal...

python Crypto模块的安装与使用方法

python Crypto模块的安装与使用方法

前言 最开始想尝试在windows下面安装python3.6,虽然python安装成功,但在安装Cryto模块用pip3 install pycrypto老是会报错。老夫搞了半天,最终决...