Chinaunix首页 | 论坛 | 博客
  • 博客访问: 55892
  • 博文数量: 11
  • 博客积分: 47
  • 博客等级: 民兵
  • 技术积分: 87
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-10 10:24
个人简介

不学无术

文章分类

全部博文(11)

文章存档

2015年(8)

2013年(1)

2012年(2)

我的朋友

分类: Python/Ruby

2015-03-10 15:21:56

替换oldtxt,newtxt
for line in fileinput.input("filepath",inplace=1):
    line=line.replace("oldtxt",“newtxt”)
    print line
例子2(

f1 = file("hello.txt", "r")
f2 = file("myhello.txt", "w")

for s in f1.readlines():
      f2.writes(s.replace("hello","hi")) #调用replace函数将s中的“hello”替换为“hi”,
f1.close()
f2.close()


)

修改某行:
with open("fo.txt","r+") as f:
    old=f.read()
    f.seek(0)
    f.write("new line\n" + old)
line before
阅读(8079) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~