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实现对PDF文件的OCR识别

基于Python实现对PDF文件的OCR识别

最近在做一个项目的时候,需要将PDF文件作为输入,从中输出文本,然后将文本存入数据库中。为此,我找寻了很久的解决方案,最终才确定使用tesseract。所以不要浪费时间了,我们开始吧。...

详解Python3迁移接口变化采坑记

1、除法相关 在python3之前, print 13/4 #result=3 然而在这之后,却变了! print(13 / 4) #result=3.25 "/”符号运算...

Python中threading模块join函数用法实例分析

本文实例讲述了Python中threading模块join函数用法。分享给大家供大家参考。具体分析如下: join的作用是众所周知的,阻塞进程直到线程执行完毕。通用的做法是我们启动一批线...

使用python编写批量卸载手机中安装的android应用脚本

该脚本的功能是卸载android手机中安装的所有第三方应用,主要是使用adb shell pm、adb uninstall 命令,所以使用的前提是需要配好adb的环境变量,下面上代码:...

解决Python下imread,imwrite不支持中文的问题

由以下函数代替该功能: def cv_imread(file_path): cv_img=cv2.imdecode(np.fromfile(file_path,dtype=np.u...