Coder Social home page Coder Social logo

Tabulators break formatting about prettytable HOT 6 OPEN

jazzband avatar jazzband commented on June 27, 2024
Tabulators break formatting

from prettytable.

Comments (6)

hugovk avatar hugovk commented on June 27, 2024

How are you actually using PrettyTable? Please provide some example Python code.

from prettytable.

dremerb avatar dremerb commented on June 27, 2024
        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.

hugovk avatar hugovk commented on June 27, 2024

Thanks, please could you create a "Minimal, Reproducible Example"?

https://stackoverflow.com/help/minimal-reproducible-example

from prettytable.

dremerb avatar dremerb commented on June 27, 2024
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.

hugovk avatar hugovk commented on June 27, 2024

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.

dremerb avatar dremerb commented on June 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.