Greg Greg - 1 year ago 92
Python Question

Python textwrap Library - How to Preserve Line Breaks?

When using Python's textwrap library, how can I turn this:

short line,

long line xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

into this:

short line,

long line xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

I tried:

w = textwrap.TextWrapper(width=90,break_long_words=False)
body = '\n'.join(w.wrap(body))

But I get:

short line, long line xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

(spacing not exact in my examples)

Answer Source


w = textwrap.TextWrapper(width=90,break_long_words=False,replace_whitespace=False)

that seemed to fix the problem for me

I worked that out from what I read here (I've never used textwrap before)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download