joining files

Dave Angel davea at ieee.org
Sun May 16 04:25:59 EDT 2010


mannu jha 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
>
>
>   
Do you have a spec?  Have you added any code to the last assignment to 
deal with this question, and in what way isn't it working?  Why don't 
you post your code?

Generally, you seem to have lines where the first "word" is a key to the 
line.  The word appears to be distinguished by whitespace.  So finding 
the key from a line would be just   line.split()[0]    Then you build a 
dictionary from each file.  You didn't specify whether a given file 
might have multiple lines with the same key, so I'll just say to watch 
out for that, as a dictionary will cheerfully overwrite entries with new 
ones.

Since your rule on multiple files is apparently to throw out any line 
whose key isn't in all files, you'd need to make a dictionary for each 
file, then analyze all  of them in a later pass.  That pass could 
involve iterating through one of the dictionaries, and for each key 
deciding if it's in all of the others.  One way to do that is to build a 
list and run all() on it.

DaveA






More information about the Python-list mailing list