Re: str() should convert ANY object to a string without EXCEPTIONS !
- From: est <electronixtar@xxxxxxxxx>
- Date: Sun, 28 Sep 2008 02:21:18 -0700 (PDT)
On Sep 28, 4:38 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
On Sat, 27 Sep 2008 22:37:09 -0700, est wrote:
Traceback (most recent call last):str(u'\ue863')
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\ue863' in
position 0
: ordinal not in range(128)
FAIL.
What result did you expect?
[...]
The problem is, why the f**k set ASCII encoding to range(128) ????????
while str() is internally byte array it should be handled in range(256)
!!!!!!!!!!
To quote Terry Pratchett:
"What sort of person," said Salzella patiently, "sits down and
*writes* a maniacal laugh? And all those exclamation marks, you
notice? Five? A sure sign of someone who wears his underpants
on his head." -- (Terry Pratchett, Maskerade)
In any case, even if the ASCII encoding used all 256 possible bytes, you
still have a problem. Your unicode string is a single character with
ordinal value 59491:
ord(u'\ue863')
59491
You can't fit 59491 (or more) characters into 256, so obviously some
unicode chars aren't going to fit into ASCII without some sort of
encoding. You show that yourself:
u'\ue863'.encode('mbcs') # Windows only
But of course 'mbcs' is only one possible encoding. There are others.
Python refuses to guess which encoding you want. Here's another:
u'\ue863'.encode('utf-8')
--
Steven
OK, I am tired of arguing these things since python 3.0 fixed it
somehow.
Can anyone tell me how to customize a default encoding, let's say
'ansi' which handles range(256) ?
.
- Follow-Ups:
- References:
- str() should convert ANY object to a string without EXCEPTIONS !
- From: est
- Re: str() should convert ANY object to a string without EXCEPTIONS !
- From: Steven D'Aprano
- str() should convert ANY object to a string without EXCEPTIONS !
- Prev by Date: Re: str() should convert ANY object to a string without EXCEPTIONS !
- Next by Date: Calculating timespan
- Previous by thread: Re: str() should convert ANY object to a string without EXCEPTIONS !
- Next by thread: Re: str() should convert ANY object to a string without EXCEPTIONS !
- Index(es):
Relevant Pages
|