dict problem

Alistair King alistair.king at helsinki.fi
Mon Oct 30 13:15:13 CET 2006


Fredrik Lundh wrote:
> Alistair King wrote:
>
>   
>> Is there any other way of removing double and single quotes from a
>> number, as a string, to give the float value again?
>>     
>
> help(str) describes what you can do with a string (an object of type 
> 'str', that is).  among the methods listed, you'll find:
>
>   
>>  |  strip(...)
>>  |      S.strip([chars]) -> string or unicode
>>  |
>>  |      Return a copy of the string S with leading and trailing
>>  |      whitespace removed.
>>  |      If chars is given and not None, remove characters in chars instead.
>>  |      If chars is unicode, S will be converted to unicode before stripping
>>     
>
> which looks like it should be pretty useful for this specific case:
>
>  >>> value = "'0.064250000000001084'"
>  >>> value
> "'0.064250000000001084'"
>  >>> value.strip("'")
> '0.064250000000001084'
>  >>> value.strip("'\"")
> '0.064250000000001084'
>  >>> float(value.strip("'\""))
> 0.064250000000001084
>
> </F>
>
>   
Thanks..

the code works great now. I know these things are quite simple to learn
from books etc.. but i would be lost without this mailinglist, from lack
of time. Hopefully soon i can give something more complicated.
I ended up doing the dictionary formatting properly and the new code is:
..............................................................................................................................
heavy = raw_input("\n\n@@@@@@@@@@@@@@@@@@\n\nPlease enter the heaviest
atom for which you obtained percentage values for, but not Oxygen or
Hydrogen, ie, 'C', 'N', 'S', 'Br'...: ")

def updateDS1v(Fxas, x):
if Fxas !=0 and DS1v.get(x)!=None:
value = DSvalues.get(heavy)
floatvalue = float(value)
atoms = DS1v.get(x) + Fxas*floatvalue
else:
value = DSvalues.get(heavy)
floatvalue = float(value)
DS1v[x] = Fxas*floatvalue

updateDS1v(FCas, 'C')
updateDS1v(FHas, 'H')
updateDS1v(FOas, 'O')
updateDS1v(FNas, 'N')
updateDS1v(FSas, 'S')
updateDS1v(FClas, 'Cl')
updateDS1v(FBras, 'Br')
updateDS1v(FZnas, 'Zn')

..............................................................................................................................
it works perfectly now

thanks for the help

a

-- 
Dr. Alistair King
Research Chemist,
Laboratory of Organic Chemistry,
Department of Chemistry,
Faculty of Science
P.O. Box 55 (A.I. Virtasen aukio 1)
FIN-00014 University of Helsinki
Tel. +358 9 191 50392, Mobile +358 (0)50 5279446
Fax +358 9 191 50366 




More information about the Python-list mailing list