question about the textwrap module

T

TP

Hi everybody,

Recently, I have tried to improve the look of the printed text in command
line. For this, I was compelled to remove redundant spaces in strings,
because in my scripts, often the strings are spreading on several lines.

For example, "aaa bbb" had to be transformed in "aaa bbb".
I have coded some simple functions to do that.

Today, by examining Python documentation, I have found an interesting
module:

http://www.python.org/doc/2.5.2/lib/module-textwrap.html

But, I haven't found any way to do my redundant space deletion with this
module? Am I right?

Thanks

Julien

--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
 
G

Gabriel Genellina

Recently, I have tried to improve the look of the printed text in command
line. For this, I was compelled to remove redundant spaces in strings,
because in my scripts, often the strings are spreading on several lines.

For example, "aaa bbb" had to be transformed in "aaa bbb".
I have coded some simple functions to do that.

Today, by examining Python documentation, I have found an interesting
module:

http://www.python.org/doc/2.5.2/lib/module-textwrap.html

But, I haven't found any way to do my redundant space deletion with this
module? Am I right?

You may pre-process your text (stripping redundant whitespace) before
using textwrap:

py> text = 'This is some \t text with multiple\n\n spaces.'
py> print textwrap.fill(text, width=20)
This is
some text with
multiple spaces.
py> import re
py> re.sub(r'\s+', ' ', text)
'This is some text with multiple spaces.'
py> t2 = _
py> print textwrap.fill(t2, width=20)
This is some text
with multiple
spaces.
py>
 
T

TP

Gabriel said:
You may pre-process your text (stripping redundant whitespace) before
using textwrap:

Thanks Gabriel for your answers!
I finally have subclassed textwrap.TextWrapper.

Julien


--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
 
S

Sion Arrowsmith

Gabriel Genellina said:
py> text = 'This is some \t text with multiple\n\n spaces.'
py> import re
py> re.sub(r'\s+', ' ', text)
'This is some text with multiple spaces.'

py> ' '.join(text.split())
'This is some text with multiple spaces.'
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top