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,对于爬虫比较感兴趣,就做了一个简单的爬虫项目,使用Python的库Tkinsert做了一个界面,感觉这个库使用起来还是挺方便的,音乐的数据来自网易云音...

pyqt4教程之实现windows窗口小示例分享

复制代码 代码如下:import sysfrom PyQt4 import QtGui, QtCoreclass Window( QtGui.QMainWindow): &nb...

python Pexpect 实现输密码 scp 拷贝的方法

在服务器A上的程序用到服务器B上的文件data,并且需要定期更新文件。 但是直接在bash文件中使用 scp -P 1000 192.168.199.10:/temp/data /t...

对Django的restful用法详解(自带的增删改查)

对Django的restful用法详解(自带的增删改查)

什么是rest REST是所有Web应用都应该遵守的架构设计指导原则。 Representational State Transfer,翻译是”表现层状态转化”。 面向资源是REST最明...

pytorch 在sequential中使用view来reshape的例子

pytorch中view是tensor方法,然而在sequential中包装的是nn.module的子类, 因此需要自己定义一个方法: import torch.nn as nn c...