extracting numbers with decimal places from a string

Joel Goldstick joel.goldstick at gmail.com
Mon Jan 12 00:35:22 CET 2015


On Sun, Jan 11, 2015 at 6:12 PM, Thomas 'PointedEars' Lahn
<PointedEars at web.de> wrote:
> Joel Goldstick wrote:
>
>> Thomas 'PointedEars' Lahn wrote:
>>> Joel Goldstick wrote:
>>>> my_list = "1.23, 2.4, 3.123".split(",")
>>>>
>>>> that will give you ['1.23', '2.4', '3.123']
>>>
>>> No, it gives
>>>
>>> […]
>>> | >>> my_list = "1.23, 2.4, 3.123".split(",")
>>> | >>> my_list
>>> | ['1.23', ' 2.4', ' 3.123']
>                ^       ^
>>> | >>>
>>>
>>> In order to get the result you described, one needs at least
>>>
>>> | >>> '1.23, 2.4, 3.123'.split(', ')
>>> | ['1.23', '2.4', '3.123']
>>>
>>> […]
>>
>> I'm not sure what you are trying to point out as your examples confirm
>> my code.
>
> No, they don't.
>
>> Am I missing something.
>                         ^
> (Is that a question.)
>
> You are missing a leading space character because in the string the comma
> was followed by one.

I see that now.  Performing float on each element of the list will
take care of that, or I guess .strip() on each first.
>
>> As for feeding a beginner regex solutions, I'm on the side that this
>> is a terrible idea.  Regex is not something a beginner should worry
>> about!
>
> NAK.
>
> --
> PointedEars
>
> Twitter: @PointedEars2
> Please do not cc me. / Bitte keine Kopien per E-Mail.
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com



More information about the Python-list mailing list