LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1039|回复: 1

想写一个替换文件中符合特定正则规则的小程序

[复制链接]
发表于 2007-4-17 16:39:55 | 显示全部楼层 |阅读模式
初学python,想写一个程序,把一个特定文件中的符合某正则规则的字符串去掉,请问改怎么写

假设文件为 f:\test.txt ,需要把文件中每行以 / 开头直到行尾的字符串去掉,改如何实现,谢谢
 楼主| 发表于 2007-4-18 09:53:57 | 显示全部楼层
写了这么一个小程序,能工作了,但请大家:
1.帮我找找写得不好的地方;
2.如果我不想用临时文件,而是打开一个文件,修改后,写回原文件,该怎么实现?
谢谢

[php]
import re, os, sys
input = file("emotions.ini", "r")
output = file("tmp.ini","w")

p = re.compile(r'/.*')

for s in input.xreadlines():
    output.write(p.sub('',s))
   
output.close(  )
input.close(  )

os.remove("emotions.ini")
os.rename("tmp.ini", "emotions.ini")
[/php]
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表