float to unicode question

vincent wehren v.wehren at home.nl
Wed Feb 12 19:32:40 CET 2003


"gabor" <gabor at z10n.net> schrieb im Newsbeitrag
news:mailman.1045070147.27747.python-list at python.org...
> hi,
>
> this works:
> a = str(14.34)
>
> this works:
> a = unicode(14.34)
>
> this doesn't work:
> a = unicode(14.34,'iso-8859-1')
>
> why?
> i have to do:
> a = unicode(string(14.34),'iso-8859-1')
>
> of course there is no sense in specifying an encoding for a float
> number, but in my program i'm dealing with all kind of data, not just
> numbers, so i have to do:
>
> a = unicode(str(something),'iso-8859-1'),
> because i don't know what type something is.
>
> the question is why does it work to convert a float to unicode without
> specifying the codepage, but it fails when codepage specified?

See the built-in-funcs documentation:
<snip>
If encoding and/or errors are given, unicode() will decode the object which
can either be an 8-bit string or a character buffer using the codec for
encoding.
<snip>
If no optional parameters are given, unicode() will mimic the behaviour of
str() except that it returns Unicode strings instead of 8-bit strings....

Of course, this may not answer the "why?" part....

Regards
Vincent Wehren


> thanks,
> gabor
>
>






More information about the Python-list mailing list