I have an origin file (fo.log) like below:
title1 title2 title3
o11 o12 o13
o21 o22 o23
o31 o32 o33
And a destination file (fd.log) like below:
d11 d12
d21 d22
d31 d32
Both files have the same number of lines (that could be millions of lines), except for the title line of the origin file. Thinking about memory usage, I don't want to read all lines to memory.
After processing my script, I would like to have the destination file (fd.log) like below:
d11 d12 o13
d21 d22 o23
d31 d32 o33
which means that I took the last information of each origin file line and appended it to the corresponding destination line.
Correspondence between lines from one file to another is just the line position and there is nothing to do with the information on it.
The closest script I could do is written bellow and it correctly prints the information I want.
from pathlib import Path
file_from = Path("path-to-origin-file/fo.log").open()
file_to = Path("path-to-destination-file/fd.log").open()
# create an enumerator to iterate over origin file lines
eft = enumerate(file_to)
# skip the first line with titles
next(eft)
for line_counter,line_to in eft:
print(' '.join([
line_to.rstrip('\n'),
file_from.readline().split()[2]]))
file_from.close()
file_to.close()