How to wrap a Japanese text in Python

  • Thread starter prashantkisanpatil
  • Start date
P

prashantkisanpatil

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
 
L

Leo Kislov

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
 
L

Leo Kislov

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top