473,544 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Line Wrapping

All,

Mail messages should be wrapped at 78 characters (as suggested in RFC
2822). I want my python batch scripts/cron jobs to enforce this
behavior, and format the mail that is sent out so that newline
characters are inserted as appropriate to keep line lengths at 78
characters or less. I wrote a function to take a string and format it
in this manner, but it seemed a bit awkward and un-Python like (my
background is mostly C, and the function looks very much like C code).
I'd imagine that there's probably a short, succinct, more elegant
solution using regular expressions or some module that is unknown to
me. Does anyone have any clean, short solutions to this problem?

--
Evan Klitzke <ev**@yelp.co m>
Jun 18 '07 #1
2 3652
Evan Klitzke wrote:
All,

Mail messages should be wrapped at 78 characters (as suggested in RFC
2822). I want my python batch scripts/cron jobs to enforce this
behavior, and format the mail that is sent out so that newline
characters are inserted as appropriate to keep line lengths at 78
characters or less. I wrote a function to take a string and format it
in this manner, but it seemed a bit awkward and un-Python like (my
background is mostly C, and the function looks very much like C code).
I'd imagine that there's probably a short, succinct, more elegant
solution using regular expressions or some module that is unknown to
me. Does anyone have any clean, short solutions to this problem?
Does textwrap module has what you need?
>>import textwrap
textwrap.wrap(' aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaa',
78)
['aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaa',
'aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaa']
>>textwrap.wrap ('This is a very long String. A linewrap module has
couple functions which might help you to accoplish your goal. HTH', 78)
['This is a very long String. A linewrap module has couple functions
which might', 'help you to accoplish your goal. HTH']
Jun 18 '07 #2
On 6/18/07, TeroV <te***@nowhere. invalidwrote:
Evan Klitzke wrote:
All,

Mail messages should be wrapped at 78 characters (as suggested in RFC
2822). I want my python batch scripts/cron jobs to enforce this
behavior, and format the mail that is sent out so that newline
characters are inserted as appropriate to keep line lengths at 78
characters or less. I wrote a function to take a string and format it
in this manner, but it seemed a bit awkward and un-Python like (my
background is mostly C, and the function looks very much like C code).
I'd imagine that there's probably a short, succinct, more elegant
solution using regular expressions or some module that is unknown to
me. Does anyone have any clean, short solutions to this problem?

Does textwrap module has what you need?
>>import textwrap
>>>
textwrap.wrap(' aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaa',
78)
['aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaa',
'aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaa']
>>textwrap.wrap ('This is a very long String. A linewrap module has
couple functions which might help you to accoplish your goal. HTH', 78)
['This is a very long String. A linewrap module has couple functions
which might', 'help you to accoplish your goal. HTH']
This sounds like exactly what I was looking for -- thanks!

--
Evan Klitzke <ev**@yelp.co m>
Jun 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2458
by: Alex Dribin | last post by:
Hi, Are there ready solutions for auto-wrapping lines in ostream? I am using ostream for output and want this output to be wrapped at some position. Of course I can monitor stream pointer position and insert end-of-lines periodically. Something better than that? Alex
11
6055
by: name | last post by:
Here is a first attempt at a line/word wrapping utility. Seems to work okay, but lacks some checking stuff, etc. --------------------------------------------------------- #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h>
4
2796
by: Brian Gideon | last post by:
All, I want to know how others deal with long lines of code. Do you care? If so, what rule(s) do you follow? Is it important to you how it looks on printed paper or displayed on the screen? Being able to keep all of the code on the screen without scrolling horizontally is important to me, but the 80 character rule is limiting and...
1
1755
by: mg | last post by:
I set the width of a WebForm Label but do not get line wrapping (see below). What can I do to get line wrapping WebForm1.asp <asp:Labe id="Label1" style="Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 48px runat="server" Width="150px" Height="64px"></asp:Label
2
2630
by: Amelyan | last post by:
If I have spaces in my Text property of Label, and I add it to TableCell, it wraps at spaces. E.g. Label lb = new Label(); lb.Text = "Hello, World!" TableCell cell = new TableCell(); cell.Controls.Add(lb); The resulting TableCell html render will look like this
6
1165
by: Markus Ernst | last post by:
Hi Searching for a possibility to display some text with preserved white space and line breaks, but with long lines being wrapped, I found this CSS declaration, which I found helpful: pre { white-space: pre-wrap; /* css-3 */ white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ white-space: -pre-wrap; /* Opera 4-6 */
5
6398
by: Jason | last post by:
I have a div box with a border, and some padding left and right. It has some text with different font sizes, and a few icons. I want to cut off any overflow at the end of the line with "...", instead of the text wrapping. What is the best way I can detect whether the line wrapped, and how many characters to cut off at the end? - Jason
7
2430
by: John den Haan | last post by:
Hello! When I use putchar to fill up an entire screen (of 80x25) with text, it seems to leave an empty line at the end, thus forcing me to scroll upwards in to see the first line. This forces me to repositioning the cursor to the first line, which costs computer power. I know the loss is negligible, but it's more a matter of principle: how...
10
6958
by: marss | last post by:
<hr style="height:1px"/does not fit because although a line looks like it has 1 pixel with but there are empty spaces above and below line. It can be seen if place two HR elements beside. <hr style="height:1px"/> <hr style="height:1px"/> I tried also <div style="height:1px;font-size:1px;background- color:black;"></divbut a line was 2...
4
2010
by: elukkien | last post by:
Hello! I'm trying to find a way to print out a long string (>400 characters no spaces, tabs or newlines) to a line width of 60 characters. So after every 60 characters a newline would start. Is it possible to transform the string to set the linewidth? for example for a linewidth of 2:
0
7414
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...
0
7598
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7757
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...
0
5895
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...
1
5288
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...
0
4905
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3400
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3398
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.