Search code examples
python-3.xfiletextreadlines

How to copy and place some text in specific place in file with python?


i need incert and copy line with date into other lines of file related to this date.

This is file that i have,

*  2019  7  8  0  0  0.00000000
PE01 -29225.002062  -4252.742792  -1915.262327   -642.336192
PE02  29239.941616   4214.548704   1856.791406     68.271625
PE03  19764.216366   9479.291699  19908.151379   -185.782224
PE04 -13231.353497  11725.555035 -23729.152873   -318.666487
PE05  14751.386626  25291.823093   4375.095547   -468.399018
PE07  -1119.264400 -26235.644241  13662.554654   -222.074278
PE08  12962.777246 -11868.521398  23825.978428   6327.578380
PE09   1464.297128  26224.237620 -13655.240745   6385.654050
PE11  -9700.857653  25890.523336 -10555.310714   5659.403360

*  2019  7  8  0  5  0.00000000
PE01 -29157.478195  -4205.546295  -2830.114774   -642.338623
PE02  29174.220890   4167.267005   2771.685480     68.272131
PE03  20017.069289  10012.927817  19387.676411   -185.783486
PE04 -13530.293652  11074.197778 -23872.919410   -318.668867
PE05  14809.564246  25394.936573   3489.967606   -468.398037

and i need to copy that date infront of every line below related to this date, something like this

2019  7  8  0  0  0.00000000 PE01 -29157.478195  -4205.546295  -2830.114774   -642.338623
2019  7  8  0  0  0.00000000 PE02  29239.941616   4214.548704   1856.791406     68.271625

how can i do that? I managed turn this file into list of lists with this

with open('example1.txt') as f:
    ex = []
    for line in f:
        if line.startswith('*  2019'):
            ex.append([])
        ex[-1].extend(line.split())

and this is what i get

[['*', '2019', '7', '8', '0', '0', '0.00000000', 'PE01', '-29225.002062', '-4252.742792', '-1915.262327', '-642.336192', 'PE02', '29239.941616', '4214.548704', '1856.791406', '68.271625', 'PE03', '19764.216366', '9479.291699', '19908.151379', '-185.782224', 'PE04', '-13231.353497', '11725.555035', '-23729.152873', '-318.666487', 'PE05', '14751.386626', '25291.823093', '4375.095547', '-468.399018', 'PE07', '-1119.264400', '-26235.644241', '13662.554654', '-222.074278'...], ['*', '2019', '7', '8', '0', '5', '0.00000000'...]]

Solution

  • This is one approach.

    Ex:

    result = []
    base_val = ''
    with open(filename) as infile:
        for line in infile:
            line = line.strip()
            if line:  #Check if line is empty
                if line.startswith("*"):
                    base_val = line.lstrip("*").strip().split()   #Get Date line
                else:
                    result.append(base_val + line.split())        #Form required list
    print(result)
    

    Output:

    [
        ['2019', '7', '8', '0', '0', '0.00000000', 'PE01', '-29225.002062', '-4252.742792', '-1915.262327', '-642.336192'], 
        ['2019', '7', '8', '0', '0', '0.00000000', 'PE02', '29239.941616', '4214.548704', '1856.791406', '68.271625'], 
        ['2019', '7', '8', '0', '0', '0.00000000', 'PE03', '19764.216366', '9479.291699', '19908.151379', '-185.782224'], 
        ['2019', '7', '8', '0', '0', '0.00000000', 'PE04', '-13231.353497', '11725.555035', '-23729.152873', '-318.666487'], 
        ['2019', '7', '8', '0', '0', '0.00000000', 'PE05', '14751.386626', '25291.823093', '4375.095547', '-468.399018'], 
        ['2019', '7', '8', '0', '0', '0.00000000', 'PE07', '-1119.264400', '-26235.644241', '13662.554654', '-222.074278'], 
        ['2019', '7', '8', '0', '0', '0.00000000', 'PE08', '12962.777246', '-11868.521398', '23825.978428', '6327.578380'], 
        ['2019', '7', '8', '0', '0', '0.00000000', 'PE09', '1464.297128', '26224.237620', '-13655.240745', '6385.654050'], 
        ['2019', '7', '8', '0', '0', '0.00000000', 'PE11', '-9700.857653', '25890.523336', '-10555.310714', '5659.403360'], 
        ['2019', '7', '8', '0', '5', '0.00000000', 'PE01', '-29157.478195', '-4205.546295', '-2830.114774', '-642.338623'], 
        ['2019', '7', '8', '0', '5', '0.00000000', 'PE02', '29174.220890', '4167.267005', '2771.685480', '68.272131'], 
        ['2019', '7', '8', '0', '5', '0.00000000', 'PE03', '20017.069289', '10012.927817', '19387.676411', '-185.783486'], 
        ['2019', '7', '8', '0', '5', '0.00000000', 'PE04', '-13530.293652', '11074.197778', '-23872.919410', '-318.668867'], 
        ['2019', '7', '8', '0', '5', '0.00000000', 'PE05', '14809.564246', '25394.936573', '3489.967606', '-468.398037']
    ]