如何将单个文件中的特定列重复替换为另一个文件的列?

How to replace specific columns in a single file repeatedly into another file's columns?

我是 python 编码新手,我有这两个文本文件需要处理。有谁知道如何将一个文件的特定列替换为另一个文件列?所以,例如, 我想获取 'test1.txt'、

的最后四列
  1 N1          -3.8340    -1.0640     2.8770 n3         1 UNL      -0.696600
  2 N2          -2.7490    -1.5690     2.2220 n3         1 UNL      -0.278400
  3 C1          -2.3750    -0.9950     1.1200 c3         1 UNL       0.169400
  4 C2          -1.2280    -1.5720     0.2740 c3         1 UNL       0.671800

并且仅将第一条文本的最后四列替换为'test2.txt'——注意重复时,倒数第三列的整数会增加。

  1 N1          31.2480    39.4030    91.8950 N.3        1 UNL       0.000000
  2 N2          32.0980    38.3940    91.5460 N.2        1 UNL       0.000000
  3 C1          33.0530    38.6590    90.7070 C.2        1 UNL       0.000000
  4 C2          33.9820    37.5500    90.1880 C.2        1 UNL       0.000000
  5 N1          55.1040    41.1430    27.6790 N.3        2 UNL       0.000000
  6 N2          53.9860    41.7250    27.1570 N.2        2 UNL       0.000000
  7 C1          53.7640    41.5940    25.8850 C.2        2 UNL       0.000000
  8 C2          52.5820    42.3090    25.2080 C.2        2 UNL       0.000000

这样最后的结果就变成了

  1 N1          31.2480    39.4030    91.8950 n3         1 UNL      -0.696600
  2 N2          32.0980    38.3940    91.5460 n3         1 UNL      -0.278400
  3 C1          33.0530    38.6590    90.7070 c3         1 UNL       0.169400
  4 C2          33.9820    37.5500    90.1880 c3         1 UNL       0.671800
  5 N1          55.1040    41.1430    27.6790 n3         2 UNL      -0.696600
  6 N2          53.9860    41.7250    27.1570 n3         2 UNL      -0.278400
  7 C1          53.7640    41.5940    25.8850 c3         2 UNL       0.169400
  8 C2          52.5820    42.3090    25.2080 c3         2 UNL       0.671800

像这样... python 编码甚至有可能吗? 这两个文件以两个不同的文件名保存。

我真的不明白这些值 <37, 38, 39, 40> 是从哪里来的,在所需结果的第一列的下 4 行中。我忽略了那些并假设这些值不应该被替换。

下面的 my_cycle() 函数 不是 设计为任何可迭代的通用目的,它在这里只是为了帮助我们 text1.txt。尽管可以出于其他目的对其进行修改。我试图让 itertools.cycle() 的修改版本在每个循环后更新特定值,在本例中,是 test1.txt 右侧的第 3 列。要更好地了解 itertools.cycle(),请阅读 this post。 Python 文档总是有帮助的。

def update_targeted_column(element):
    list_elem = element.split()
    new_element = '    '.join(list_elem[:-3] + [str(int(list_elem[-3]) + 1)] + list_elem[-2:])
    return '\n    ' + new_element


def my_cycle(iterable):
    """After each iteration of all rows, my_cycle() should increment the 3rd right-most column by 1"""
    saved = []
    for element in iterable:
        yield element
        saved.append(update_targeted_column(element))
    while saved:
        for element in saved:
            yield element
            saved.append(update_targeted_column(element))


# Combining the two files into a third one
with open('f1.txt', 'r') as file_01:
    with open('f2.txt', 'r') as file_02:
        with open('f3.txt', 'a+') as file_03:
            cycled = my_cycle(file_01.readlines())
            for line_01, line_02 in zip(cycled, file_02.readlines()):
                last = '    '.join(line_01.split()[-4:])    # Separating the 4 right-most columns from lines of file_01
                first = '    '.join(line_02.split()[:5])    # Separating the 5 left-most columns from lines of file_02
                print(first + '    ' + last)                # Joining the separated columns to get expected result
                # file_03.write(first + '    ' + last + '\n')

输出(在第 3 个文件中):

1    N1    31.2480    39.4030    91.8950    n3    1    UNL    -0.696600
2    N2    32.0980    38.3940    91.5460    n3    1    UNL    -0.278400
3    C1    33.0530    38.6590    90.7070    c3    1    UNL    0.169400
4    C2    33.9820    37.5500    90.1880    c3    1    UNL    0.671800
5    N1    55.1040    41.1430    27.6790    n3    2    UNL    -0.696600
6    N2    53.9860    41.7250    27.1570    n3    2    UNL    -0.278400
7    C1    53.7640    41.5940    25.8850    c3    2    UNL    0.169400
8    C2    52.5820    42.3090    25.2080    c3    2    UNL    0.671800