我想用给定的字符串替换文本文件中的特定文本部分。例如,给定以下文件内容:
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