[Tutor] mergin two csv files based on a common join

W W srilyk at gmail.com
Sat Nov 1 00:09:24 CET 2008


On Fri, Oct 31, 2008 at 4:04 PM, qsqgeekyogdty at tiscali.co.uk <
qsqgeekyogdty at tiscali.co.uk> wrote:

> Hello again,
> Thanks for the replies on my previous post, but I have a different
> problem now and don't see how to deal with it in a smooth way.


I'd probably use a dict with a list as the value:


> <snip>
> "1", "text", "aa"
> "1", "text", "xx"
> "2", "text2", "something else"
> "3", "text3", "something else"
> "3", "text3", "zz"
>

this is untested, and it's been a while since I've messed with the CSV
module, but something like this should work, if you split the first file
into 3 lists and the 2nd into two lists:
for col1, col2, col3 in zip(inCol1, inCol2, inCol3):
    try:
        mydict[(col1, col2)]
        mydict[(col1, col2)].append(col3)
    except KeyError:
        mydict[(col1, col2)] = [col3]

I think that should work.

HTH,
Wayne


-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn't. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081031/943acfd6/attachment-0001.htm>


More information about the Tutor mailing list