0

我想用给定的字符串替换文本文件中的特定文本部分。例如,给定以下文件内容:

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
    
4

2 回答 2

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---'))。

于 2013-07-31T13:35:32.750 回答
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 标记,写出原始行,然后是你想要的行,然后,忽略所有直到结束标记,并写下剩余的行(跳过第一行,因为它将是结尾标记)...

于 2013-07-31T13:41:30.863 回答