Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
xxxxxxxxxxxxxxxxxxx

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
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

(spacing not exact in my examples)

like image 978
Greg Avatar asked Jul 22 '09 15:07

Greg


People also ask

How do you wrap text in Python output?

wrap(text, width=70, **kwargs): This function wraps the input paragraph such that each line in the paragraph is at most width characters long. The wrap method returns a list of output lines. The returned list is empty if the wrapped output has no content.

How do you wrap text in idle Python?

Put simply, if you type a very long piece of text, it disappears off the side of the page, unlike this comment box where it wraps the text around into many lines. If you use the return key to wrap the text, instead of starting a new line, it runs the program.

What is Dedent in Python?

textwrap. dedent (text) Remove any common leading whitespace from every line in text. This can be used to make triple-quoted strings line up with the left edge of the display, while still presenting them in the source code in indented form.


4 Answers

try

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)

like image 156
Peter Avatar answered Oct 09 '22 19:10

Peter


body = '\n'.join(['\n'.join(textwrap.wrap(line, 90,
                 break_long_words=False, replace_whitespace=False))
                 for line in body.splitlines() if line.strip() != ''])
like image 20
far Avatar answered Oct 09 '22 18:10

far


How about wrap only lines longer then 90 characters?

new_body = ""
lines = body.split("\n")

for line in lines:
    if len(line) > 90:
        w = textwrap.TextWrapper(width=90, break_long_words=False)
        line = '\n'.join(w.wrap(line))

    new_body += line + "\n"
like image 6
tefozi Avatar answered Oct 09 '22 18:10

tefozi


TextWrapper is not designed to handle text that already has newlines in it.

There are a two things you may want to do when your document already has newlines:

1) Keep old newlines, and only wrap lines that are longer than the limit.

You can subclass TextWrapper as follows:

class DocumentWrapper(textwrap.TextWrapper):

    def wrap(self, text):
        split_text = text.split('\n')
        lines = [line for para in split_text for line in textwrap.TextWrapper.wrap(self, para)]
        return lines

Then use it the same way as textwrap:

d = DocumentWrapper(width=90)
wrapped_str = d.fill(original_str)

Gives you:

short line,
long line xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxx

2) Remove the old newlines and wrap everything.

original_str.replace('\n', '')
wrapped_str = textwrap.fill(original_str, width=90)

Gives you

short line,  long line xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

(TextWrapper doesn't do either of these - it just ignores the existing newlines, which leads to a weirdly formatted result)

like image 4
Peter Avatar answered Oct 09 '22 18:10

Peter