[Tutor] how to read two files and substitute

Emile van Sebille emile at fenx.com
Tue May 17 19:51:50 CEST 2011


On 5/17/2011 9:47 AM lina said...
> A further question:  I don't know how can I get the final output is unique?

That'll require some rework.  The mapping container replaces
mapping[parts[0]] each time it encounters another parts[0].
You can test if you are about to add a new entry or replace
an existing one with "if parts[0] in mapping:" if that helps.

Emile




>
> #!/bin/python
>
> mapping={}
> for line in open("confout.pdb").readlines():
>      parts=line.strip().split()
>      if len(parts)>6:
>          mapping[parts[1]]=parts[4]+parts[3]
> origs=open("dummy.atomID").read().split()
> print " ".join([mapping[orig] for orig in origs])
>
> Thanks again,
>
>
>
> On Wed, May 18, 2011 at 12:36 AM, lina<lina.lastname at gmail.com>  wrote:
>> Thanks, it works.
>>
>> On Tue, May 17, 2011 at 11:53 PM, Emile van Sebille<emile at fenx.com>  wrote:
>>> On 5/17/2011 8:31 AM lina said...
>>>>
>>>> Following Michiel's code (a little adjustment was done):
>>>>
>>>
>>> Well. you're almost there.  The error you're getting
>>> is likely due to splitting an empty line, then referencing
>>> the [1] and [4] elements.
>>>
>>> After you split the line into parts, test to confirm
>>> that the fields you need are there:
>>>
>>>> #!/bin/python
>>>>
>>>> mapping={}
>>>> for line in open("confout.pdb").readlines():
>>>>    parts=line.strip().split()
>>>
>>>      if len(parts)>3:
>>>
>>>>    mapping[parts[1]]=parts[4]
>>>
>>> also, note that python indexes from zero, so this should be
>>>      mapping[parts[0]]=parts[3]
>>>
>>>> origs=open("dummy.atomID").read().split()
>>>> print " ".join([mapping[orig] for orig in origs])
>>>
>>>
>>> Emile
>>>
>>> _______________________________________________
>>> Tutor maillist  -  Tutor at python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>
>>
>>
>> --
>> Best Regards,
>>
>> lina
>>
>
>
>




More information about the Tutor mailing list