473,765 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about the textwrap module

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
3 1240
En Mon, 27 Oct 2008 20:50:08 -0200, TP <Tr**********@p aralleles.inval id>
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(t ext, 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(t 2, width=20)
This is some text
with multiple
spaces.
py>

--
Gabriel Genellina

Oct 27 '08 #2
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.TextWr apper.

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
Gabriel Genellina <ga*******@yaho o.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.spl it())
'This is some text with multiple spaces.'

--
\S -- si***@chiark.gr eenend.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1630
by: Kevin MacKenzie | last post by:
I'm a complete newbie to using Python. I have a small question about importing modules. Is there any difference between the two following statements, and what (if any) are they? >>> from Module import * and
0
1228
by: David M. Wilson | last post by:
Hello! I maintain a small package for talking to the API of BulkSMS.co.uk. I have been adding support for some new features recently, and found myself slightly indecisive over how best to lay out the modules internally. I have a 'BulkSMS.Exceptions' module, which contains a base class and (currently) all derived exceptions that my package throws. This has been fine up until now, as the exceptions were generated by only one
1
1557
by: Joshua Burvill | last post by:
Hello, How can I use the textwrap (new in 2.3) module if I have to use python 2.1? Rgds, Josh
6
2046
by: Steven Bethard | last post by:
So I've recently been making pretty frequent use of textwrap.dedent() to allow me to use triple-quoted strings at indented levels of code without getting the extra spaces prefixed to each line. I discovered today that not only does textwrap.dedent() strip any leading spaces, but it also substitutes any internal tabs with spaces. For example:: py> def test(): .... x = ('abcd efgh\n' .... 'ijkl mnop\n') .... y =...
21
1350
by: Hari Sekhon | last post by:
Is it better to do: message = """This is line1. This is line2 This is line3\n""" or message = "This is line1.\n message = message + "This is line2\n"
15
2924
by: Tom Plunket | last post by:
The documentation for dedent says, "Remove any whitespace than can be uniformly removed from the left of every line in `text`", yet I'm finding that it's also modifying the '\t' characters, which is highly undesirable in my application. Is there any way to stop it from doing this, or alternatively, to put those tabs back in? I see that TextWrap and its member functions take an 'expand_tabs' kwarg, but dedent unfortunately does not. ...
6
4031
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new types of objects (classes) can only be defined in Class modules.
0
931
by: Jesse Aldridge | last post by:
So in the code below, I'm binding some events to a text control in wxPython. The way I've been doing it is demonstrated with the Lame_Event_Widget class. I want to factor out the repeating patterns. Cool_Event_Widget is my attempt at this. It pretty much works, but I have a feeling there's a better way of doing this. Also, I couldn't get the Cool_Text_Control to override the on_text method. How would I do that? -------------
0
962
by: Gary Herron | last post by:
Dan Yamins wrote: Because loading (and reloading) assigns values to variables (called binding a value in Python), but does not go on a hunt to find variables to *unbind*. Once a variable is bound to a value, it stays bound until something unbinds or rebinds it, and module loading does no such thing. But in fact you did *not* delete the module from memory. A module
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.