[Tutor] csv merge with different column title

Alan Gauld alan.gauld at yahoo.co.uk
Sat Apr 14 04:40:41 EDT 2018


On 14/04/18 04:32, Pareshkumar Panchal wrote:

> I am trying to merge two csv files with following condition.
> 
> filea.csv column( a1,a2,a3)
> fileb.csv column( b1,b2,b3)
> 
> i wanted to merge if a1 & b1 reference (as the title is different but
> contents are valid for comparison). also the result i need is
> a1,a2,a3,b1,b2 (not b3).

This doesn't have much to do with the data being CSV but
applies to merging any data structures. Lets start by
assuming the data is small enough to read both filers
into memory as two lists. (If not the same principles
apply but you have to do it from disk which will be
slower)

First read your two lists A and B.
Sort both lists based on the first field.
Start at the top of list A and search for a corresponding item in B.
step through B processing each item until the field no longer matches
step onto the next item in A and repeat the above process

When you reach the end of either list you are done.

There are some other (cleverer) ways to do this but that
simple approach is how batch data processing has worked
for the last 60 years. It's simple to write and debug.

Try it, if it doesn't work for you show us what you've
tried and any errors.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list