[Tutor] how to fill zero value and join two column

Alan Gauld alan.gauld at btinternet.com
Thu Jul 23 01:22:37 CEST 2009


<amrita at iisermohali.ac.in> wrote 

> I have two text file, having entries as
> fileA
> 38 ALA H = 8.29 N = 120.62 CA = 54.33 HA = 4.04 C = 178.95
> 8 ALA H = 7.85  N = 123.95 CA = 54.67 HA =  C =
> fileB
> 8 ALA  helix (helix_alpha, helix1)
> 21 ALA  helix (helix_alpha, helix2)
> ...
> those atoms which doesnot have nay value in fileA. so the reult will be
> something like:-
> 
> fileC
> 8 ALA H = 7.85  N = 123.95 CA = 54.67 HA =0.00  C =0.00|8 ALA  helix
> (helix_alpha, helix1)

There is a similar problem discussed a few days ago on tis list, 
try looking over the archive for this week to get some ideas.

> I tried to merge these two files using commands like:-
> 
> from collections import defaultdict
>>>> def merge(sources):
> ...   if __name__ == "__main__":

You should not use this inside a function, nor at the >>> prompt. 
This is used to determine wheher a file should be treated as a 
module or main program. As it stands the rest of your code will 
never be executed since the __name__ at the >>> prompt 
is never __main__

Looks like you need to go back to fundamentals for a spell.


> ...        a = open("/home/amrita/alachems/chem100.txt")
> ...        c = open("/home/amrita/secstr/secstr100.txt")
> ...        def source(stream):
> ...            return (line.strip() for line in stream)
> ...        for m in merge([source(x) for x in [a,c]]):
> ...            print "|".join(c.ljust(10) for c in m)

I haven't quite got my head around the logic here but it 
looks to me like an overly elaborate solution. You could 
simplify the last for loop to just do

for m in merge([source(a),source(c)])

But of course that is a recursive function call to merge() and I 
can't see any terminating condition so it should loop forever 
(or until it reaches the recursion limit)...

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list