Comments (6)
How are you actually using PrettyTable? Please provide some example Python code.
from prettytable.
startindex = page * num_elems
showarr = inputarr[startindex:startindex+num_elems]
headers = ["Index", "Date", "Type", "Command", "Internal Hash", "Project Commit Hash", "Diff from last commit", "Tag"]
table = PrettyTable()
table.field_names = headers
# this line is currently used to remove "\t" chars from problematic column, breaks without
table.add_rows(map(lambda x: (x[0], x[1], x[2], x[3], x[4], x[5], x[6].replace("\t", ""), x[7]), showarr))
table.align = "l"
print(table)
from prettytable.
Thanks, please could you create a "Minimal, Reproducible Example"?
https://stackoverflow.com/help/minimal-reproducible-example
from prettytable.
from prettytable import PrettyTable
data = [[ "first",
"second",
"""diff --git partdiff.c partdiff.c
index 05ced0c..804cd7d 100644
--- partdiff.c
+++ partdiff.c
@@ -377,6 +377,7 @@ main (int argc, char** argv)
\tstruct calculation_arguments arguments;
\tstruct calculation_results results;
+\tprintf("Some random text!");
\taskParams(&options, argc, argv);
\tinitVariables(&arguments, &results, &options);""",
"fourth"],
]
headers = ["col1", "col2", "col3", "col4"]
table = PrettyTable()
table.field_names = headers
table.add_rows(data)
print(table)
produces
+-------+--------+--------------------------------------------------+--------+
| col1 | col2 | col3 | col4 |
+-------+--------+--------------------------------------------------+--------+
| first | second | diff --git partdiff.c partdiff.c | fourth |
| | | index 05ced0c..804cd7d 100644 | |
| | | --- partdiff.c | |
| | | +++ partdiff.c | |
| | | @@ -377,6 +377,7 @@ main (int argc, char** argv) | |
| | | struct calculation_arguments arguments; | |
| | | struct calculation_results results; | |
| | | | |
| | | + printf("Some random text!"); | |
| | | askParams(&options, argc, argv); | |
| | | | |
| | | initVariables(&arguments, &results, &options); | |
+-------+--------+--------------------------------------------------+--------+
You could also try adding a table.align = "l"
, shifts content correctly inside the column, but delimiters are broken in exactly the same way.
from prettytable.
Thanks for that!
So it looks like this is the Python's print
reading the \t
as a tab character.
For example:
>>> print("a\tb")
a b
>>> print(" \tstruct calculation_arguments arguments;")
struct calculation_arguments arguments;
If I preprocess the data and replace \t
with \\t
, to escape the backslash:
from prettytable import PrettyTable
data = [[ "first",
"second",
"""diff --git partdiff.c partdiff.c
index 05ced0c..804cd7d 100644
--- partdiff.c
+++ partdiff.c
@@ -377,6 +377,7 @@ main (int argc, char** argv)
\\tstruct calculation_arguments arguments;
\\tstruct calculation_results results;
+\\tprintf("Some random text!");
\\taskParams(&options, argc, argv);
\\tinitVariables(&arguments, &results, &options);""",
"fourth"],
]
headers = ["col1", "col2", "col3", "col4"]
table = PrettyTable()
table.field_names = headers
table.add_rows(data)
print(table)
It prints nicely as:
+-------+--------+---------------------------------------------------+--------+
| col1 | col2 | col3 | col4 |
+-------+--------+---------------------------------------------------+--------+
| first | second | diff --git partdiff.c partdiff.c | fourth |
| | | index 05ced0c..804cd7d 100644 | |
| | | --- partdiff.c | |
| | | +++ partdiff.c | |
| | | @@ -377,6 +377,7 @@ main (int argc, char** argv) | |
| | | \tstruct calculation_arguments arguments; | |
| | | \tstruct calculation_results results; | |
| | | | |
| | | +\tprintf("Some random text!"); | |
| | | \taskParams(&options, argc, argv); | |
| | | | |
| | | \tinitVariables(&arguments, &results, &options); | |
+-------+--------+---------------------------------------------------+--------+
But I guess it doesn't make much sense to show \t
in the output.
Another idea is to pre-process the \t
as four spaces, or two spaces, whatever you prefer:
from prettytable import PrettyTable
data = [[ "first",
"second",
"""diff --git partdiff.c partdiff.c
index 05ced0c..804cd7d 100644
--- partdiff.c
+++ partdiff.c
@@ -377,6 +377,7 @@ main (int argc, char** argv)
struct calculation_arguments arguments;
struct calculation_results results;
+ printf("Some random text!");
askParams(&options, argc, argv);
initVariables(&arguments, &results, &options);""",
"fourth"],
]
headers = ["col1", "col2", "col3", "col4"]
table = PrettyTable()
table.field_names = headers
table.add_rows(data)
print(table)
Produces:
+-------+--------+-----------------------------------------------------+--------+
| col1 | col2 | col3 | col4 |
+-------+--------+-----------------------------------------------------+--------+
| first | second | diff --git partdiff.c partdiff.c | fourth |
| | | index 05ced0c..804cd7d 100644 | |
| | | --- partdiff.c | |
| | | +++ partdiff.c | |
| | | @@ -377,6 +377,7 @@ main (int argc, char** argv) | |
| | | struct calculation_arguments arguments; | |
| | | struct calculation_results results; | |
| | | | |
| | | + printf("Some random text!"); | |
| | | askParams(&options, argc, argv); | |
| | | | |
| | | initVariables(&arguments, &results, &options); | |
+-------+--------+-----------------------------------------------------+--------+
How does that sound? Can you preprocess \t
into 2 or 4 (or whatever) spaces?
I'm not sure if this should be handled by PrettyTable itself, whether it should make assumptions whether \t
should be 2 or 4 spaces. Or if the centering algorithm can be adapted to somehow take into account a tab, or how it should do that.
from prettytable.
Printing \t
as a character does not make sense in my case, as it's simply for formatting, no information.
Imo PrettyTable could handle the replacing, makes for a better user experience - just throw in some data and it'll work. While I found the workaround pretty quickly, might not be the case for a Python beginner. For me \t
is 4 spaces, but you could introduce an optional parameter to set it to whatever value.
But you could also argue that the user has to sanitize their data. In that case maybe document the behavior somewhere, so that the fix can be found easily?
from prettytable.
Related Issues (20)
- Support for rich colored cell data HOT 2
- Support displaying selected fields for CSV format
- Pretty printing inside cloudwatch logs HOT 1
- Is max_width word-wrapping configurable? HOT 10
- PrettyTable.add_rows method support divider Argument
- Calculate column width HOT 4
- Full-width characters cannot be wrapped correctly. HOT 2
- can you add an option for the divider, so insted of add the divider below it add its above?
- Negative Integer and Float in Red and positive in Green
- argument max_width doesn't work
- 3.10.0: pytest fails because incorrect module name HOT 2
- max_table_width does not work as expected HOT 7
- Colortable with title leads to broken layout HOT 6
- Add color to field names HOT 4
- Print table failed when I created a copy.py in the same directory with my other python files. HOT 1
- Markdown tables with narrow columns don't render
- Mediawiki Output
- Cannot release from Jazzband to PyPI HOT 3
- `float_format` not working HOT 1
- `float_format` not working HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from prettytable.