Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process two files at the same time in Python

Tags:

python

string

I have information about 12340 cars. This info is stored sequentially in two different files:

  1. car_names.txt, which contains one line for the name of each car
  2. car_descriptions.txt, which contains the descriptions of each car. So 40 lines for each one, where the 6th line reads @CAR_NAME

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

like image 484
Open the way Avatar asked Nov 30 '22 06:11

Open the way


1 Answers

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
like image 133
Robert Rossney Avatar answered Dec 01 '22 20:12

Robert Rossney