[Tutor] just one question
Rich Lovely
roadierich at googlemail.com
Thu Jul 16 12:56:46 CEST 2009
2009/7/16 <amrita at iisermohali.ac.in>:
> Thanks for your help I tried your commands like:---
>
> from __future__ import with_statement #only works on version 2.5 and later
> from collections import defaultdict
> from decimal import Decimal
>
> atoms = defaultdict(dict)
>
> with open("file1.txt") as f:
> for line in f:
> n, pos, ala, at, symb, weight, rad, count = line.split()
> atoms[int(pos)][at] = Decimal(weight)
> atomsNeeded = ("C", "CA", "CB")
>
> for k, v in atoms.iteritems():
> print k, "ALA C = %s CA = %s CB = %s" %tuple(v.get(a,"") for a in
> atomsNeeded)
>
> but it is giving error like:----
>
> Traceback (most recent call last):
> File "a1.py", line 9, in <module>
> n, pos, ala, at, symb, weight, rad, count = line.split()
> ValueError: need more than 0 values to unpack
>
> What I should do.
>
>
>
>> 2009/7/15 <amrita at iisermohali.ac.in>:
>>> It is working but the only problem is that it is not taking the values
>>> of
>>> C, CA and CB from fifth column in front of them i tried in this way
>>> like:
>>>
>>>>>> from __future__ import with_statement
>>>>>> from collections import defaultdict
>>>>>> from decimal import Decimal
>>>>>> atoms = defaultdict(dict)
>>>>>> with open("file1.txt") as f:
>>> ... for line in f:
>>> ... n, pos, ala, at, symb, weight, rad, count = line.split()
>>> ... atoms[int(pos)][at] = Decimal(weight)
>>> ... positionsNeeded = (8, 15, 21)
>>> ... atomsNeeded = ("C", "CA", "CB")
>>> ... for position in positionsNeeded:
>>> ... print position, "ALA C = %s CA = %s CB = %s" %
>>> tuple(atoms[position].get(a,"values[5]") for a in atomsNeeded)
>>> ...
>>> 8 ALA C = values[5] CA = values[5] CB = values[5]
>>> 15 ALA C = values[5] CA = values[5] CB = values[5]
>>> 21 ALA C = values[5] CA = values[5] CB = values[5]
>>> Traceback (most recent call last):
>>> File "<stdin>", line 3, in <module>
>>> ValueError: need more than 0 values to unpack
>>>
>>> I want that whatever be the corresponding value of C,CA and CB is there
>>> in
>>> 5th column it will write that, and one more thing that the input file i
>>> am
>>> having is very big so i can't its 5th column values, but position it
>>> should mention
>>>
>>> so if my file is something like this:---
>>>
>>> Amrita Kumari
>>> Research Fellow
>>> IISER Mohali
>>> Chandigarh
>>> INDIA
>>>
>>>
>>
>> So what you're saying, (I think) is you don't want to have the
>> positionsNeeded line.
>>
>> The problem with the code you tried, is that you've got the last for
>> loop indented, whereas it wasn't in my code. You have also changed ""
>> into "values[5]", and you wonder why it's appearing in your output?
>>
>> Change the final for loop to the following, and it will do what I
>> think you want it to do.
>>
>> for k, v in atoms.iteritems():
>> print k, "ALA C = %s CA = %s CB = %s" %tuple(v.get(a,"") for a in
>> atomsNeeded)
>>
>> Make sure it is at the left hand margin, with no spaces before the "for".
>>
>> Otherwise, think carefully about what you want your program to do, and
>> how you can explain it, because I'm not sure anyone here really knows
>> what you want your code to do.
>>
>> Also, please don't fill up the email with any more examples of input
>> and output. We've seen it three times now, I think we know what it
>> should look like in that sense.
>>
>> --
>> Rich "Roadie Rich" Lovely
>> There are 10 types of people in the world: those who know binary,
>> those who do not, and those who are off by one.
>>
>
>
> Amrita Kumari
> Research Fellow
> IISER Mohali
> Chandigarh
> INDIA
>
>
That sounds like there is a blank line in the input file. Therefire
it should be safe to catch the exception and ignore it.
Change the top of the with/for blocks as follows:
with open("file1.txt") as f:
for line in f:
try:
n, pos, ala, at, symb, weight, rad, count = line.split()
except ValueError:
continue
else:
atoms[int(pos)][at] = Decimal(weight)
--
Rich "Roadie Rich" Lovely
There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.
More information about the Tutor
mailing list