[Tutor] Converting from unicode to nonstring

Steven D'Aprano steve at pearwood.info
Fri Oct 15 04:27:31 CEST 2010


On Fri, 15 Oct 2010 05:19:57 am Sander Sweers wrote:

> So you want convert string u'1,2,3,4' to a list of ints [1,2,3,4]?
> Then the below will work.
>
> [int(n) for n in u'1,2,3,4'.replace(',', '')]

That will break if you have u'100,2,3,4'.

Better is:


>>> s = '1, 200 , -3,4'  # or whatever
>>> [int(x) for x in s.split(',')]
[1, 200, -3, 4]




-- 
Steven D'Aprano


More information about the Tutor mailing list