如何在python的顺序单词中插入制表符



我有一个非常大的文本文件,看起来像下面的问题:

A T T A G C A
A AT A G C A
T TT AG G A
G T T A G C A

每个字符都由t分割,但有些字符是连接的,我想将t添加到这些序列中。我需要的内容如下:

A T T A G C A
A A T A G C A
T T T A G C A
G T T A G C A

我可以用Python做什么?我需要充分利用我的电脑内存来加快这个过程。

假设输入存储在in.txt中,一个优雅的解决方案是

import re
with open('in.txt') as fin, open('out.txt', 'w') as fout:
    for line in fin:
        fout.write('t'.join(re.findall('w', line))+'n')

输出保存在out.txt文件中

我可能会像这样写一个原始文件的副本。

with open('in.txt') as input, open('out.txt', 'w') as output:
    prev_char = None
    while True:
        c = input.read(1)
        if not c:
            break
        if prev_char and prev_char != 't' and c != 't':
            output.write('t')
        output.write(c)
        prev_char = c

最新更新