Re: How does unicode() work?

2008-01-09 Thread Robert Latest
John Machin wrote: > When mixing unicode strings with byte strings, Python attempts to > decode the str object to unicode, not encode the unicode object to > str. Thanks for the explanation. Of course I didn't want to mix Unicode and Latin in one string, my snippet just tried to illustrate the p

Re: How does unicode() work?

2008-01-09 Thread John Machin
On Jan 10, 1:55 am, Carsten Haese <[EMAIL PROTECTED]> wrote: > On Wed, 2008-01-09 at 15:33 +0100, Fredrik Lundh wrote: > > When mixing Unicode with byte strings, Python attempts to decode the > > byte string, not encode the Unicode string. > > Ah, I did not realize that. I never mix Unicode and byt

Re: How does unicode() work?

2008-01-09 Thread Carsten Haese
On Wed, 2008-01-09 at 15:33 +0100, Fredrik Lundh wrote: > When mixing Unicode with byte strings, Python attempts to decode the > byte string, not encode the Unicode string. Ah, I did not realize that. I never mix Unicode and byte strings in the first place, and now I know why. Thanks for clearing

Re: How does unicode() work?

2008-01-09 Thread Fredrik Lundh
Carsten Haese wrote: > If that really is the line that barfs, wouldn't it make more sense to > repr() the unicode object in the second position? > > import sys > for k in sys.stdin: > print '%s -> %s' % (k, repr(k.decode('iso-8859-1'))) > > Also, I'm not sure if the OP has told us the truth

Re: How does unicode() work?

2008-01-09 Thread Carsten Haese
On Wed, 2008-01-09 at 13:44 +0100, Fredrik Lundh wrote: > Robert Latest wrote: > > > Here's a test snippet... > > > > import sys > > for k in sys.stdin: > > print '%s -> %s' % (k, k.decode('iso-8859-1')) > > > > ...but it barfs when actually fed with iso8859-1 characters. How is this > > do

Re: How does unicode() work?

2008-01-09 Thread Fredrik Lundh
Robert Latest wrote: > Here's a test snippet... > > import sys > for k in sys.stdin: > print '%s -> %s' % (k, k.decode('iso-8859-1')) > > ...but it barfs when actually fed with iso8859-1 characters. How is this > done right? it's '%s -> %s' % (byte string, unicode string) that barfs. try

Re: How does unicode() work?

2008-01-09 Thread Robert Latest
Robert Latest wrote: > ...but it barfs when actually fed with iso8859-1 characters. Specifically, it says: UnicodeDecodeError: 'ascii' codec can't decode byte 0xf6 in position 0: ordinal not in range(128) which doesn't make sense to me, because I specifically asked for the iso8859-1 decoder, n

How does unicode() work?

2008-01-09 Thread Robert Latest
Here's a test snippet... import sys for k in sys.stdin: print '%s -> %s' % (k, k.decode('iso-8859-1')) ...but it barfs when actually fed with iso8859-1 characters. How is this done right? robert -- http://mail.python.org/mailman/listinfo/python-list