Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

String formatting for complex writing systems

0 views
Skip to first unread message

Andy

unread,
Jun 27, 2007, 3:20:52 AM6/27/07
to
Hi guys,

I'm writing a piece of software for some Thai friend. At the end it
is supposed to print on paper some report with tables of text and
numbers. When I test it in English, the columns are aligned nicely,
but when he tests it with Thai data, the columns are all crooked.

The problem here is that in the Thai writing system some times two or
more characters together might take one single space, for example งิ
(u"\u0E07\u0E34"). This is why when I use something like u"%10s"
% ..., it just doesn't work as expected.

Is anybody aware of an alternative string format function that can
deal with this kind of writing properly?

Any suggestion is highly appreciated. Thanks!
Andy

Gabriel Genellina

unread,
Jun 27, 2007, 3:47:19 AM6/27/07
to pytho...@python.org

The same thing happens even in English if you print using a proportional
width font, a "W" is usually wider than an "i" or "l" letter.
You could use a reporting library or program (like ReportLab, generating
PDF files), but perhaps the simplest approach is to generate an HTML page
containing a table, and display and print it using your favorite browser.

--
Gabriel Genellina

Leo Kislov

unread,
Jun 27, 2007, 6:10:00 AM6/27/07
to

In general case it's impossible to write such a function for many
unicode characters without feedback from rendering library.
Assuming you use *fixed* font for English and Thai the following
function will return how many columns your text will use:

from unicodedata import category
def columns(self, s):
return sum(1 for c in s if category(c) != 'Mn')

-- Leo

Leo Kislov

unread,
Jun 27, 2007, 6:14:47 AM6/27/07
to

That should of course be written as def columns(s). Need to learn to
proofread before posting :)

-- Leo

Andy

unread,
Jul 2, 2007, 6:22:32 AM7/2/07
to
Thanks guys!

I've used the HTML and the unicodedata suggestions, each on a
different report. These worked nicely!

Andy

0 new messages