Re: how to make format operator % work with unicode as expected

2008-01-27 Thread John Machin
On Jan 27, 3:06 pm, Peter Pei [EMAIL PROTECTED] wrote:
 I probably should mention that what I want is to make all parts of the
 string aligned, and look like table. I am not looking for other ways to make
 it table-alike, but only interested in making % work with unicode -counting
 characters not bytes...

Can you show some *code* that demonstrates the alleged problem?
E.g. len(some format % some_unicode) != expected_len

Reading this may help:
http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to make format operator % work with unicode as expected

2008-01-27 Thread Stefan Behnel
Ever heard the word PLONK?

Peter Pei harshly top-posted:
 You didn't understand my question, but thanks any way.
 
 Yes, it is true that %s already support unicode, and I did not
 contradict that. But it counts the number of bytes instead of
 characters, and makes things like %-20s out of alignment. If you don't
 understand my assertion, please don't argue back and I am only
 interested in answers from those who are qualified.
 ==
 
 Steven D'Aprano [EMAIL PROTECTED] wrote
[pretty qualifying response stripped]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to make format operator % work with unicode as expected

2008-01-27 Thread Marc 'BlackJack' Rintsch
On Sun, 27 Jan 2008 05:32:40 +, Peter Pei wrote:

 You didn't understand my question, but thanks any way.
 
 Yes, it is true that %s already support unicode, and I did not contradict 
 that. But it counts the number of bytes instead of characters, and makes 
 things like %-20s out of alignment. If you don't understand my assertion, 
 please don't argue back and I am only interested in answers from those who 
 are qualified.

I have the impression from your original post

  […] because it is unicode, and one byte is not neccessary one character.

that you confuse unicode and utf-8.  Are you sure you are qualified to ask
such a question in the first place!?  :-þ

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: how to make format operator % work with unicode as expected

2008-01-27 Thread Colin J. Williams
Peter Pei wrote:
 You didn't understand my question, but thanks any way.
 
 Yes, it is true that %s already support unicode, and I did not 
 contradict that. But it counts the number of bytes instead of 
 characters, and makes things like %-20s out of alignment. If you don't 
 understand my assertion, please don't argue back and I am only 
 interested in answers from those who are qualified.


Peter,

Rudeness is inappropriate whether the 
person being attacked is
a frequent or infrequent contributer to 
this list.

Colin W
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to make format operator % work with unicode as expected

2008-01-27 Thread Diez B. Roggisch
Peter Pei schrieb:
 I am using things like %-20s%-60s%-10s in tkinter listbox to make it 
 look like a table, with mono sized font like lucie system. But this does 
 not work with data contains Les misérables, because it is unicode, and 
 one byte is not neccessary one character. Now how can I resolve this issue?

By learning that unicode is not UTF-8, instead of insulting others of 
being incompetent.


Diezz
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to make format operator % work with unicode as expected

2008-01-27 Thread Peter Pei

I V [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 On Sun, 27 Jan 2008 05:32:40 +, Peter Pei wrote:
 Yes, it is true that %s already support unicode, and I did not
 contradict that. But it counts the number of bytes instead of
 characters, and makes things like %-20s out of alignment. If you don't
 understand my assertion, please don't argue back and I am only
 interested in answers from those who are qualified.

 What version of python are you using? On python 2.4 and 2.5 on linux,
2.5.2 on windows, if you have tested linux, maybe... windows is the issue?
 %-20s counts the characters, not the bytes, or, I think it does. when I
 run:

 print u'%-20s|\n%-20s|' % (u'foo bar', u'foo bár')

 the output is:

 foo bar |
 foo bár |
 

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: how to make format operator % work with unicode as expected

2008-01-27 Thread Peter Pei

Steven D'Aprano [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 On Sun, 27 Jan 2008 05:32:40 +, Peter Pei wrote:

 You didn't understand my question, but thanks any way.

 Yes, it is true that %s already support unicode, and I did not
 contradict that. But it counts the number of bytes instead of
 characters, and makes things like %-20s out of alignment. If you don't
 understand my assertion, please don't argue back and I am only
 interested in answers from those who are qualified.

 I understand your assertion. I think it is nonsense.
this kind of reply only embarrase yourself

 def test():
 ... print 12345678901234567890 +
 ... print %-20s + % Plain ASCII
 ... print u%-20s + % uLes misérables-\320\321\322
 ...
 test()
 12345678901234567890 +
 Plain ASCII  +
 Les misérables-ÐÑÒ   +




 -- 
 Steven 

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: how to make format operator % work with unicode as expected

2008-01-27 Thread Peter Pei

Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 On Sun, 27 Jan 2008 05:32:40 +, Peter Pei wrote:

 You didn't understand my question, but thanks any way.

 Yes, it is true that %s already support unicode, and I did not contradict
 that. But it counts the number of bytes instead of characters, and makes
 things like %-20s out of alignment. If you don't understand my assertion,
 please don't argue back and I am only interested in answers from those 
 who
 are qualified.

 I have the impression from your original post

  […] because it is unicode, and one byte is not neccessary one character.

 that you confuse unicode and utf-8.  Are you sure you are qualified to ask
 such a question in the first place!?  :-þ
so you are saying, with utf-8 encoding a byte is a character, shame on you.

 Ciao,
 Marc 'BlackJack' Rintsch 

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: how to make format operator % work with unicode as expected

2008-01-27 Thread Marc 'BlackJack' Rintsch
On Sun, 27 Jan 2008 16:00:42 +, Peter Pei wrote:

 Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote in message 
 news:[EMAIL PROTECTED]
 On Sun, 27 Jan 2008 05:32:40 +, Peter Pei wrote:

 You didn't understand my question, but thanks any way.

 Yes, it is true that %s already support unicode, and I did not contradict
 that. But it counts the number of bytes instead of characters, and makes
 things like %-20s out of alignment. If you don't understand my assertion,
 please don't argue back and I am only interested in answers from those 
 who are qualified.

 I have the impression from your original post

  […] because it is unicode, and one byte is not neccessary one character.

 that you confuse unicode and utf-8.  Are you sure you are qualified to
 ask such a question in the first place!?  :-þ
 so you are saying, with utf-8 encoding a byte is a character, shame on
 you.

No I don't say that.  I say unicode has no bytes but codepoints.  And with
unicode objects Python counts characters and not bytes.  So I guess you
are trying to format utf-8 encoded byte strings instead of unicode
strings.  Because with unicode strings your problem simply does not
exist.  As several people already *showed* to you with examples.  Once
again:

In [346]: u = u'sm\xf8rebr\xf8d'

In [347]: s = u.encode('utf-8')

In [348]: print '%-20s+\n%-20s+' % (s, 'spam')
smørebrød +
spam+

In [349]: print '%-20s+\n%-20s+' % (u, 'spam')
smørebrød   +
spam+

348 is what you are doing, utf-8 encoded byte strings, but you claim that's
a problem with unicode.

And 349 is formatting unicode.  See, no problem -- lines up nicely.

Instead of embarrassing yourself and being rude to people you should take
some time and learn something about unicode and encodings.  Especially that
utf-8 ≠ unicode.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list

how to make format operator % work with unicode as expected

2008-01-26 Thread Peter Pei
I am using things like %-20s%-60s%-10s in tkinter listbox to make it look 
like a table, with mono sized font like lucie system. But this does not work 
with data contains Les misérables, because it is unicode, and one byte is 
not neccessary one character. Now how can I resolve this issue?

My issue is how to make format operator % work with unicode as expected, 
and has nothing to do with tkinter. If I want to use a table widget or 
something, I can. But that's not the question. 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to make format operator % work with unicode as expected

2008-01-26 Thread Peter Pei
I probably should mention that what I want is to make all parts of the
string aligned, and look like table. I am not looking for other ways to make
it table-alike, but only interested in making % work with unicode -counting
characters not bytes... 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to make format operator % work with unicode as expected

2008-01-26 Thread Steven D'Aprano
On Sun, 27 Jan 2008 04:06:45 +, Peter Pei wrote:

 I probably should mention that what I want is to make all parts of the
 string aligned, and look like table. I am not looking for other ways to
 make it table-alike, but only interested in making % work with unicode
 -counting characters not bytes...

% already works with unicode. Just give it unicode arguments:


 print ux y z %s 1 2 3 % uLes misérables
x y z Les misérables 1 2 3


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: how to make format operator % work with unicode as expected

2008-01-26 Thread Peter Pei
You didn't understand my question, but thanks any way.

Yes, it is true that %s already support unicode, and I did not contradict 
that. But it counts the number of bytes instead of characters, and makes 
things like %-20s out of alignment. If you don't understand my assertion, 
please don't argue back and I am only interested in answers from those who 
are qualified.
==

Steven D'Aprano [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 On Sun, 27 Jan 2008 04:06:45 +, Peter Pei wrote:

 I probably should mention that what I want is to make all parts of the
 string aligned, and look like table. I am not looking for other ways to
 make it table-alike, but only interested in making % work with unicode
 -counting characters not bytes...

 % already works with unicode. Just give it unicode arguments:


 print ux y z %s 1 2 3 % uLes misérables
 x y z Les misérables 1 2 3


 -- 
 Steven 

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: how to make format operator % work with unicode as expected

2008-01-26 Thread Peter Pei
I just sorted posts by from, and figured out that you are kind of PSF guy... 
However that does not make you qualified, I care whether you are capable not 
whether you have the time to spend for PSF.

Adios!
==
Peter Pei [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 You didn't understand my question, but thanks any way.

 Yes, it is true that %s already support unicode, and I did not contradict 
 that. But it counts the number of bytes instead of characters, and makes 
 things like %-20s out of alignment. If you don't understand my assertion, 
 please don't argue back and I am only interested in answers from those who 
 are qualified.
 ==

 Steven D'Aprano [EMAIL PROTECTED] wrote in message 
 news:[EMAIL PROTECTED]
 On Sun, 27 Jan 2008 04:06:45 +, Peter Pei wrote:

 I probably should mention that what I want is to make all parts of the
 string aligned, and look like table. I am not looking for other ways to
 make it table-alike, but only interested in making % work with unicode
 -counting characters not bytes...

 % already works with unicode. Just give it unicode arguments:


 print ux y z %s 1 2 3 % uLes misérables
 x y z Les misérables 1 2 3


 -- 
 Steven
 

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: how to make format operator % work with unicode as expected

2008-01-26 Thread Peter Pei
For sure I can calculate the number of characters and do the padding myself, 
but what's the point, and i surely hope that python does it for me.

Peter Pei [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
I am using things like %-20s%-60s%-10s in tkinter listbox to make it look 
like a table, with mono sized font like lucie system. But this does not 
work with data contains Les misérables, because it is unicode, and one 
byte is not neccessary one character. Now how can I resolve this issue?

 My issue is how to make format operator % work with unicode as expected, 
 and has nothing to do with tkinter. If I want to use a table widget or 
 something, I can. But that's not the question. 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to make format operator % work with unicode as expected

2008-01-26 Thread I V
On Sun, 27 Jan 2008 05:32:40 +, Peter Pei wrote:
 Yes, it is true that %s already support unicode, and I did not
 contradict that. But it counts the number of bytes instead of
 characters, and makes things like %-20s out of alignment. If you don't
 understand my assertion, please don't argue back and I am only
 interested in answers from those who are qualified.

What version of python are you using? On python 2.4 and 2.5 on linux, 
%-20s counts the characters, not the bytes, or, I think it does. when I 
run:

 print u'%-20s|\n%-20s|' % (u'foo bar', u'foo bár')

the output is:

foo bar |
foo bár |

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: how to make format operator % work with unicode as expected

2008-01-26 Thread Steven D'Aprano
On Sun, 27 Jan 2008 05:32:40 +, Peter Pei wrote:

 You didn't understand my question, but thanks any way.
 
 Yes, it is true that %s already support unicode, and I did not
 contradict that. But it counts the number of bytes instead of
 characters, and makes things like %-20s out of alignment. If you don't
 understand my assertion, please don't argue back and I am only
 interested in answers from those who are qualified.

I understand your assertion. I think it is nonsense.

 def test():
... print 12345678901234567890 +
... print %-20s + % Plain ASCII
... print u%-20s + % uLes misérables-\320\321\322
...
 test()
12345678901234567890 +
Plain ASCII  +
Les misérables-ÐÑÒ   +




-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list