Re: [Tutor] reading parts of a input string into different variables based on units.

2008-03-20 Thread Kent Johnson
Kent Johnson wrote: > One regex can split apart a numeric part and a non-numeric unit: A little explanation: > In [22]: import re > In [23]: splitter = re.compile(r'(\d+)(\S+)') The regex finds one or more digits \d+ followed by one or more non-whitespace characters \S+. The parentheses define

Re: [Tutor] reading parts of a input string into different variables based on units.

2008-03-20 Thread Alan Gauld
"Kent Johnson" <[EMAIL PROTECTED]> wrote > One regex can split apart a numeric part and a non-numeric unit: > In [24]: splitter.findall('2m 4cm 3mm') > Out[24]: [('2', 'm'), ('4', 'cm'), ('3', 'mm')] As ever Kent, a neat solution. Much more efficient than my 3 way search and relatively easy to

Re: [Tutor] reading parts of a input string into different variables based on units.

2008-03-19 Thread Marc Tompkins
On Wed, Mar 19, 2008 at 3:56 PM, Tim Michelsen <[EMAIL PROTECTED]> wrote: > m = 0 > cm = 0 > mm = 0 > ## first list item > 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")

Re: [Tutor] reading parts of a input string into different variables based on units.

2008-03-19 Thread Kent Johnson
Alan Gauld wrote: > 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. > Of course you will have to work out the right regex but that > shouldn't

Re: [Tutor] reading parts of a input string into different variables based on units.

2008-03-19 Thread Tim Michelsen
Hello, thank you for the fast reply! > 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 th

Re: [Tutor] reading parts of a input string into different variables based on units.

2008-03-19 Thread Alan Gauld
"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.

[Tutor] reading parts of a input string into different variables based on units.

2008-03-19 Thread Tim Michelsen
Hello, 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(" ") print mylist first = mylist[0] second =