468,284 Members | 1,530 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,284 developers. It's quick & easy.

How to wrap a Japanese text in Python

Hi All,

I am trying to wrap a japanese text in Python, by the following code.

if len(message) 54:
message = message.decode("UTF8")
strlist = textwrap.wrap(message,54)

After this I am wirting it to you a CAD Software window. While
displaying in this window some Japanese characters at the end of the
line & some at the begining of the line are not displayed at all.
Meaning the text wrapping is not happening correctly.

Can any body please help me out in resolving this problem.

Thanks in advance.

Regards,
Prashant

Jun 7 '07 #1
2 2291
On Jun 7, 5:12 am, pr****************@gmail.com wrote:
Hi All,

I am trying to wrap a japanese text in Python, by the following code.

if len(message) 54:
message = message.decode("UTF8")
strlist = textwrap.wrap(message,54)

After this I am wirting it to you a CAD Software window. While
displaying in this window some Japanese characters at the end of the
line & some at the begining of the line are not displayed at all.
Meaning the text wrapping is not happening correctly.

Can any body please help me out in resolving this problem.
First of all you should move message.decode('utf-8') call out of "if"
and you don't need "if" anyway because if the line is less than 54
textwrap won't touch it:

message = message.decode('utf-8')
strlist = textwrap.wrap(message, 54)

I don't know Japanese but the following example *seems* to work fine
for me:

# -*- coding: utf-8 -*-
sample=u"""
"""

import textwrap
for line in textwrap.wrap(sample, 6):
print line
--------------------------------
Result:




Can you post a short example that clearly demonstrates the problem?

-- Leo
Jun 8 '07 #2
On Jun 8, 2:24 am, Leo Kislov <Leo.Kis...@gmail.comwrote:
On Jun 7, 5:12 am, prashantkisanpa...@gmail.com wrote:
Hi All,
I am trying to wrap a japanese text in Python, by the following code.
if len(message) 54:
message = message.decode("UTF8")
strlist = textwrap.wrap(message,54)
After this I am wirting it to you a CAD Software window. While
displaying in this window some Japanese characters at the end of the
line & some at the begining of the line are not displayed at all.
Meaning the text wrapping is not happening correctly.
Can any body please help me out in resolving this problem.

First of all you should move message.decode('utf-8') call out of "if"
and you don't need "if" anyway because if the line is less than 54
textwrap won't touch it:

message = message.decode('utf-8')
strlist = textwrap.wrap(message, 54)

I don't know Japanese but the following example *seems* to work fine
for me:

# -*- coding: utf-8 -*-
sample=u"""
"""

import textwrap
for line in textwrap.wrap(sample, 6):
print line
--------------------------------
Result:
Oh, my. IE7 and/or Google groups ate my Japanese text :( But I hope
you've got the idea: try to work on a small example python program
in a unicode-friendly IDE like for example IDLE.
Can you post a short example that clearly demonstrates the problem?
This question is still valid.

-- Leo.

Jun 8 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by David Thomas | last post: by
2 posts views Thread by Robert M. Gary | last post: by
3 posts views Thread by Benoit Martin | last post: by
12 posts views Thread by paulgor | last post: by
6 posts views Thread by Victor | last post: by
12 posts views Thread by Steve Howell | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.