I have information about 12340 cars. This info is stored sequentially in two different files:
I would like to do in python: to add for each car in the car_descriptions.txt file the name of each car (which comes from the other file) in the 7th line (it is empty), just after @CAR_NAME
I thought about:
1) read 1st file and store car names in a matrix/list 2) start to read 2nd file and each time it finds the string @CAR_NAME, just write the name on the next line
But I wonder if there is a faster approach, so the program reads each time one line from each file and makes the modification.
Thanks
First, make a generator that retrieves the car name from a sequence. You could yield every 7th line; I've made mine yield whatever line follows the line that starts with @CAR_NAME
:
def car_names(seq):
yieldnext=False
for line in seq:
if yieldnext: yield line
yieldnext = line.startswith('@CAR_NAME')
Now you can use itertools.izip
to go through both sequences in parallel:
from itertools import izip
with open(r'c:\temp\cars.txt') as f1:
with open(r'c:\temp\car_names.txt') as f2:
for (c1, c2) in izip(f1, car_names(f2)):
print c1, c2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With