[Tutor] simple unicode question

Dave Angel davea at davea.name
Sat Aug 23 00:08:21 CEST 2014


Albert-Jan Roskam <fomcl at yahoo.com.dmarc.invalid> Wrote in message:
> Hi,
> 
> I have data that is either floats or byte strings in utf-8. I need to cast both to unicode strings. I am probably missing something simple, but.. in the code below, under "float", why does [B] throw an error but [A] does not?
> 
>..........

>>>> unicode(value, sys.getdefaultencoding())  # [B]
> 
> Traceback (most recent call last):
>   File "<pyshell#22>", line 1, in <module>
>     unicode(value, sys.getdefaultencoding())
> TypeError: coercing to Unicode: need string or buffer, float found
>>>> unicode(value, "utf-8")
> # (... also TypeError)
> 

Unicode has no encoding, so trying to specify one during a float
 conversion is nonsensical. 

Only byte-strings have encodings.  You decode them to Unicode, 
 and the result has no encoding.


-- 
DaveA



More information about the Tutor mailing list