question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wordwrap with long lines of text for a specific column

See original GitHub issue

I have the following code:

results = [('test', 'PR skipped', '428 A PR from test to develop is already open. Please close/merge it before you run this script. And some more text..  Skipping this module...')]
headers=("Module Name", "Result", "Comment")
print(tabulate(results,  headers, tablefmt="grid"))   

and produces this

image

is there a way I can wrap text in the last column?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mageliskcommented, Mar 2, 2021

FYI, I had a need for this so have implemented most of this so far. Will plan on a PR in in the next few days. You’re welcome to look at it early if desired. https://github.com/astanin/python-tabulate/compare/master...magelisk:90-automatic-wordwrap?expand=1

2reactions
astanincommented, Feb 20, 2021

It’s a feature I used to think about, and one day it may get implemented. Basically, tabulate() function will need an extra parameter with a list of column widths. Something like this:

print(tabulate(results, headers, tablefmt="grid", colwidths=[20, 20, 40]))
...

Meanwhile this effect can be easily achieved by using textwrap module directly, which is part of the standard library. It is as easy as writing two lines of code if you want to wrap all columns:

colwidths = [20, 20, 40]
results_wrapped = [[textwrap(cell,w) for w,cell in zip(colwidths, row)] for row in results]

And the result is what you want:

>>> print(tabulate(results_wrapped, headers, "grid"))
+---------------+------------+------------------------------------------+
| Module Name   | Result     | Comment                                  |
+===============+============+==========================================+
| test          | PR skipped | 428 A PR from test to develop is already |
|               |            | open. Please close/merge it before you   |
|               |            | run this script. And some more text..    |
|               |            | Skipping this module...                  |
+---------------+------------+------------------------------------------+

You get the idea.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I wrap text at a certain column size?
What's the best way to force a text file with long lines to be wrapped at a certain width? Bonus points if you...
Read more >
Wrap text in a cell - Microsoft Support
Microsoft Excel can wrap text so it appears on multiple lines in a cell. You can format the cell so the text wraps...
Read more >
Wrapping Long Lines - jEdit
The word wrap feature splits lines at word boundaries in order to fit text within a specified wrap margin. A word boundary, for...
Read more >
Wrapping and breaking text - CSS: Cascading Style Sheets
This property will break a word once it is too long to fit on a line by itself. Note: The overflow-wrap property acts...
Read more >
datatable word wrap one columns to two lines - Stack Overflow
Im trying to give this row better styling, and one solution I thought of was to have the row wrap to two lines...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found