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)
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.
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.
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.
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)
body = '\n'.join(['\n'.join(textwrap.wrap(line, 90,
break_long_words=False, replace_whitespace=False))
for line in body.splitlines() if line.strip() != ''])
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"
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With