python 写文件问题
这样子的,我想把一些文件中的字符串替换成新的串,这个串要写入到旧串的位置,初学py,写了一个笨拙的脚本,我想到的是用re.sub替换,发现脚本运行不了:
#!/usr/bin/python
import re
f = open('1.txt', 'r+')
for i in f.readlines():
tmp=re.compile(r'i')
new='hosts'+' '+'backup'
c=re.sub(tmp,new,f)
f.close()
请大家帮忙看下或者有没有更方便的写法,多谢了
#!/usr/bin/python
import re
f = open('1.txt', 'r+')
for i in f.readlines():
tmp=re.compile(r'i')
new='hosts'+' '+'backup'
c=re.sub(tmp,new,f)
f.close()
请大家帮忙看下或者有没有更方便的写法,多谢了
作者: 爱斯基摩寂寞 发布时间: 2011-06-03
我的意思是说 如何把新行直接写入到本地文件中老行的位置
作者: 爱斯基摩寂寞 发布时间: 2011-06-03