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

时间:2017-07-07 21:29:33

标签: python file text multiple-columns edit

我是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编码的可能性吗? 这两个文件保存在两个不同的文件名中。

1 个答案:

答案 0 :(得分:0)

我真的不明白那些值<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