我想用给定的字符串替换文本文件中的某段文本。例如,给定以下文件内容:
1
---From here---
2
---To here---
3
我想编写一个python函数,在以这种方式的格式启动时:
replaceSection('pathToFile','---From here---\n','---To here---\n','Woo Hoo!\n')
这应该将原始文件更改为:
1
Woo Hoo!
3
我已经提出了一个简单的实现(下面),但我认为它有一些缺点,我想知道是否有更简单的实现:
这与我在C ++代码中使用的实现相同,我猜Python有一些隐藏的优点可以使实现更加优雅
def replaceSection(pathToFile,sectionOpener,sectionCloser,replaceWith = ''):
'''
Delete all the lines in a certain section of the given file and put instead a customized text.
Return:
None if a replacement was performed and -1 otherwise.
'''
f = open(pathToFile,"r")
lines = f.readlines()
f.close()
if sectionOpener in lines:
isWrite = True # while we are outside the block and current line should be kept
f = open(pathToFile,"w")
#Write each line until reaching a section opener
# from which write nothing until reaching the section end.
for line in lines :
if line == sectionOpener:
isWrite = False
if isWrite:
# We are outside the undesired section and hence want to keep current line
f.write(line)
else:
if line == sectionCloser:
# It's the last line of the section
f.write(replaceWith)
)
isWrite = True
else:
# Current line is from the block we wish to delete
# so don't write it.
pass
f.flush()
f.close()
else:
return -1
答案 0 :(得分:1)
在这里你可以找到你的2个模式的位置:这会划分文本的一部分,你只需要用你的新模式替换它:
>>> f = my_file.readlines()
>>> beg = f.index('---From here---')
>>> end = f.index('---To here---') + len('---To here---')
>>> print f.replace(f[beg:end], 'Woo woo !')
1
Woo woo !
3
请注意第二个分隔符的长度(因此f.index('---To here---') + len('---To here---')
)。
答案 1 :(得分:1)
这是基于itertools
的方法:
from itertools import takewhile, dropwhile, chain, islice
with open('input') as fin, open('output', 'w') as fout:
fout.writelines(chain(
takewhile(lambda L: L != '---From here---\n', fin),
['Woo Hoo!\n'],
islice(dropwhile(lambda L: L != '---To here---\n', fin), 1, None)
)
)
所以,在我们到达from标记之前,写出原始行,然后写出你想要的行,然后忽略所有内容直到结束标记,并写下剩下的行(跳过第一行,因为它会是结束标记)...