0

I'm new to python coding, and I have this two text files that I need to manipulate. Does anyone know how to replace specific columns of one file into another files columns? So, for example, I want to take the last four columns of the '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

and replace only the last four columns of first text into 'test2.txt' --note that when it repeats it, the integer in the third to last column will increase.

  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

so that the final result becomes

  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

like this... Is this even a possibility with python coding? The two files are saved in two different filenames.

  • The data is just a txt file with spaced in between? always the same number of spaces? If you for example could save the data as a `csv` file, this would make things a lot easier. – rinkert Jul 07 '17 at 21:42
  • It's not clear to me what you want. Do you want the last column to be replaced with the values in the first table? Do you also want to replace the values in the 6th column (N.3 -> n3)? – Nathaniel Rivera Saul Jul 07 '17 at 21:51
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [on topic](http://stackoverflow.com/help/on-topic) and [how to ask](http://stackoverflow.com/help/how-to-ask) apply here. StackOverflow is not a design, coding, research, or tutorial service. – Prune Jul 07 '17 at 22:03
  • 1
    In particular, this application is not common enough to have a standardized format. You have to write the repetition code yourself. Note that all you're changing is the last column; the 3rd-to-last column is already properly incremented in `test2.txt`. Store the repeating values in a list. Use a simple counter for the lines. Each line gets its final column replaced with the value indicated by `counter % 4`. – Prune Jul 07 '17 at 22:07

1 Answers1

0

I did not really understand from where those values <37, 38, 39, 40> came from in the lower 4 rows of the 1st column of the desired result that. I ignored those and assumed that those values were not supposed to be replaced.

The following my_cycle() function is not designed to be a general purposed for any iterable, it is here only to help us only with text1.txt. Though it can be modified for other purposes. I tried to have a modified version of itertools.cycle() to update a particular value after each cycle, in this case, the 3rd column from the right of test1.txt. To have a better understanding of itertools.cycle(), go through this post. And Python documentation is always helpful.

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

Output(in a 3rd file):

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
arif
  • 524
  • 8
  • 18