By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,592 Members | 1,486 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,592 IT Pros & Developers. It's quick & easy.

question about the textwrap module

P: n/a
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/...-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)
Oct 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
En Mon, 27 Oct 2008 20:50:08 -0200, TP <Tr**********@paralleles.invalid>
escribió:
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/...-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:

pytext = 'This is some \t text with multiple\n\n spaces.'
pyprint textwrap.fill(text, width=20)
This is
some text with
multiple spaces.
pyimport re
pyre.sub(r'\s+', ' ', text)
'This is some text with multiple spaces.'
pyt2 = _
pyprint textwrap.fill(t2, width=20)
This is some text
with multiple
spaces.
py>

--
Gabriel Genellina

Oct 27 '08 #2

P: n/a
TP
Gabriel Genellina wrote:
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)
Oct 28 '08 #3

P: n/a
Gabriel Genellina <ga*******@yahoo.com.arwrote:
>pytext = 'This is some \t text with multiple\n\n spaces.'
pyimport re
pyre.sub(r'\s+', ' ', text)
'This is some text with multiple spaces.'
py' '.join(text.split())
'This is some text with multiple spaces.'

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Oct 29 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.