2017-07-07 67 views
0

我是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編碼的可能性? 這兩個文件保存在兩個不同的文件名。

+0

的數據僅僅是具有間隔一個txt文件之間?總是有相同數量的空格?如果你例如可以將數據保存爲'csv'文件,這會使事情變得更容易。 –

+0

我不清楚你想要什麼。您是否希望將最後一列替換爲第一個表中的值?你是否也想替換第6列中的值(N.3 - > n3)? –

+0

歡迎來到StackOverflow。請閱讀並遵守幫助文檔中的發佈準則。 [在主題](http://stackoverflow.com/help/on-topic)和[如何提問](http://stackoverflow.com/help/how-to-ask)適用於此處。 StackOverflow不是一個設計,編碼,研究或教程服務。 – Prune

回答

0

我真的不知道這些值<37, 38, 39, 40>從哪裏來自所需結果的第一列的低4行。我忽略了這些,並假定這些值不應該被取代。

以下my_cycle()功能設計成定意爲可迭代的一般,它是在這裏只是爲了幫助我們只有text1.txt。雖然它可以修改用於其他目的。我試圖修改版本itertools.cycle()來更新每個週期後的特定值,在這種情況下,從test1.txt的右邊第三列。要更好地瞭解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') 

輸出(在第三文件):

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 
相關問題