如何使用 python 将一些文本复制并放置在文件中的特定位置?

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

我需要 incert 并将带有日期的行复制到与此日期相关的文件的其他行中。

这是我的文件,

*  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

我需要在下面与此日期相关的每一行前面复制该日期,类似这样

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

我该怎么做? 我设法用这个

把这个文件变成列表列表
with open('example1.txt') as f:
    ex = []
    for line in f:
        if line.startswith('*  2019'):
            ex.append([])
        ex[-1].extend(line.split())

这就是我得到的

[['*', '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'...]]

这是一种方法。

例如:

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)

输出:

[
    ['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']
]