Apr-17-2019, 10:36 AM
This should work.
Some magic to remove white spaces from nested rows and using tuple unpacking.
If you can, use
Some magic to remove white spaces from nested rows and using tuple unpacking.
If you can, use
pathlib.Path
, but it's not mandatory.from pathlib import Path import csv csv_file1 = Path('car.csv') csv_file2 = Path('price.csv') new_file = Path('result.csv') with csv_file1.open() as fd_car, csv_file2.open() as fd_price,\ new_file.open('w') as fd_result: cars = csv.reader(fd_car) prices = csv.reader(fd_price) writer = csv.writer(fd_result) # skipping headers next(cars) next(prices) for rows in zip(cars, prices): # strip whitespaces (car, car_price), (old_price, new_price) = (map(lambda s: s.strip(), row) for row in rows) print('car-row:', car, car_price, 'price-row:', old_price, new_price) # compare price with old_price # remind, that you compare strings if car_price != old_price: print('Price missmatch, skipping', car) continue writer.writerow([car, new_price])
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!