Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何使用python在文件中的特定位置复制和放置一些文本?_Python 3.x_File_Text_Readlines - Fatal编程技术网

Python 3.x 如何使用python在文件中的特定位置复制和放置一些文本?

Python 3.x 如何使用python在文件中的特定位置复制和放置一些文本?,python-3.x,file,text,readlines,Python 3.x,File,Text,Readlines,我需要插入并复制带有日期的行到与此日期相关的文件的其他行中 这是我的档案 * 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.78222

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

这是我的档案

*  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'...]]
这是一种方法

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)
[
    ['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']
]
输出:

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