471,325 Members | 1,484 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

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.com>
Jun 18 '07 #1
2 3598
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('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa',
78)
['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']
>>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('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa',
78)
['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']
>>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.com>
Jun 18 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Alex Dribin | last post: by
11 posts views Thread by name | last post: by
4 posts views Thread by Brian Gideon | last post: by
1 post views Thread by mg | last post: by
2 posts views Thread by Amelyan | last post: by
6 posts views Thread by Markus Ernst | last post: by
7 posts views Thread by John den Haan | last post: by
10 posts views Thread by marss | last post: by
4 posts views Thread by elukkien | last post: by

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.