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

相关文章

浅谈python中str字符串和unicode对象字符串的拼接问题

str字符串 s = '中文' # s: <type 'str'> s是个str对象,中文字符串。存储方式是字节码。字节码是怎么存的: 如果这行代码在python解释...

python3 判断列表是一个空列表的方法

python3 判断空列表 @(python3) 有个判断列表是否为空的需求,试了好多方式,比如: a = [] if a is not None: COMMAND a =...

python聊天程序实例代码分享

代码简单,直接看代码吧:复制代码 代码如下:import socketimport threadingimport re#import Tkinter def ser(): &...

Python WSGI的深入理解

前言 本文主要介绍的是Python WSGI相关内容,主要来自以下网址: What is WSGI? WSGI Tutorial An Introduction t...

Python vtk读取并显示dicom文件示例

Python vtk读取并显示dicom文件示例

因为做项目的原因,所以接触到了医学图像dicom文件。vtk刚开始看,这里仅仅只是其最简单的读取显示功能。此处用到了vtk库,可自行百度安装方法。 下面附上代码: from vtk...