I have a chunk of text and I want to print it so that every line is exactly n
characters in width.
I'm using python's textwrap library but the problem I've had with that is that sometimes the line of text will be less than n
characters in length with no whitespace padding.
My code looks like this
from tabulate import tabulate
import textwrap
tabulate.PRESERVE_WHITESPACE = True
s = [long line here]
width = 64
s = textwrap.fill(
s,
width,
replace_whitespace=False,
break_long_words=True,
break_on_hyphens=False,
)
print(tabulate([[s]], tablefmt="grid"))
I'm printing out a series of these textboxes and it would look awkward if they were of different width. Depending on what s
is sometimes I get a box that is of a width less than width
Is there an option I'm not seeing in the textwrap library or maybe a way I can extend the library to get it to do what I want?
Here are the strings I'm setting s
to:
"Meta 335 to 95\n\nAmzn 188 to 96\n\nNflx 700 to 286\n\nSpot 305 to 80\n\nCRM 311 to 160\n\nSnap 57 to 10\n\nAmd 164 to 60\n\nARKK 126 to 39\n\nThese are all stocks off their Nov 2021 highs. Most of the market was poor phrasing, but this is some good perspective (instead of comparing vs the indices)"
"Meta 335 to 95\n\nAmzn 188 to 96\n\nNflx 700 to 286\n\nSpot 305 to 80\n\nCRM 311 to 160\n 57 to 10\n\nAmd 164 to 60\n\nARKK 126 to 39\n\nThese are all stocks off their Nov 2021 highs. Most of the market was poor phrasing, but this is some good perspective (instead of comparing vs the indices)"
"Meta 335Spot0 0nnR 1 o10n5 o1\\Ad14t 0nnRK16t rl tocks off their Nov 2021 highs. Most of the market was poor phrasing, but this is some good perspective (instead of comparing vs the indices)"
If you set s
to these strings you'll see that you get tables of different widths.