"Tim Michelsen" <[EMAIL PROTECTED]> wrote
> I would like to read several parts of a string into different
> variables
> based on the (physical) units of the quantities.
>
> Here's my testing code:
> ###############
> mystring = '2m 4cm 3mm' # can also be '1pound 30pence', ...
> mylist = mystring.split(" ")
For something like this I'd use regular expressions.
If your strings vary in length then I'd use a separate regular
expression per unit then use that to findall matching
substrings for each unit.
> if first.endswith("m",-1):
> m = first.strip("m")
> elif first.endswith("cm",-2):
> cm = first.strip("cm")
> elif first.endswith("mm",-2):
> mm = first.strip("mm")
I'd also look at using a dictionary to store the results
so that you can use the unit as a key.
> Well, I cannot get the meters assigned to the m variable, the
> centimeters to the cm variable and the milimeters to the mm
> variable.
> All units end with "m" and therefore my code confuses the strings I
> am
> looking for. I would always reassign the m variable.
A regex would avoid that since it would detect an m
as being diffrent to a cm or mm.
Of course you will have to work out the right regex but that
shouldn't be too difficult if you are sure you have a whitespace
separator and a number followed by the unit string.
The code then becomes (in pseudo code)
cm_re = # whatever
mm_re = # whatever
m_re = # whatever
units['cm'] = cm_re.findall(theString)
units['mm'] = mm_re.findall(theString)
units['m'] = m_re.findall(theString)
Now you have a list of values for each unit, you just need
to convert the string values to numeric and sum them or
whatever else you need to do.
HTH,
Alan G.
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor