Line Wrapping

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Evan Klitzke

    #1

    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 <evan@yelp.co m>
  • TeroV

    #2
    Re: Line Wrapping

    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']

    Comment

    • Evan Klitzke

      #3
      Re: Line Wrapping

      On 6/18/07, TeroV <teroV@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 <evan@yelp.co m>

      Comment

      Working...