joining files

Chris Rebert clp2 at rebertia.com
Sun May 16 04:18:25 EDT 2010


On Sun, May 16, 2010 at 12:02 AM, mannu jha <mannu_0523 at rediffmail.com> wrote:
> Hi,
>
> I have few files like this:
> file1:
> 22 110.1
> 33 331.5 22.7
> 5 271.9 17.2 33.4
> 4 55.1
>
> file1 has total 4 column but some of them are missing in few row.
>
> file2:
> 5 H
> 22 0
>
> file3:
> 4 T
> 5 B
> 22 C
> 121 S
>
> in all these files first column is the main source of matching their
> entries. So What I want in the output is only those entries which is coming
> in all three files.
> output required:
> 5 271.9 17.2 33.4 5 H 5 T
> 22 110.1 22 0 22 C

Outline of approach:
1. For each file, create a dict mapping the first number on each line
to that line.
2. Take the set intersection of the key sets of the dictionaries.
3. For each key in the intersection, get the values associated with it
from all the dicts and combine them, then output the combination.

HTH, but you're not gonna get any code out of me.
Some trivial parsing and knowledge of Python's datatypes is involved;
you should already know that or be able to readily figure it out from
the docs.

Cheers,
Chris
--
<insert Indian programmer quality joke here>
http://blog.rebertia.com



More information about the Python-list mailing list