Skip to content

Python script to create a text-formatted table of numbers that are decimal aligned, display a set number of significant figures and formatted with commas and spaces for thousands and thousanths..

Notifications You must be signed in to change notification settings

DrSkippy/Text-Format-Table

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

         FORMAT CSV FILE AS EQUALLY SPACED TEXT TABLES or LaTeX TABLES 
                                    - and -
   CONTROL SIGNIFICANT FIGURES DIPSLAYED on TABLE, COLUMN OR INDIVIDUAL VALUES


This is a simple Python text formatting project. After searching for
this capability for a little while, I decided to write my own as the
following requirements seemed to be unique:

* Create a table of numbers from a CSV file
* Control the number of significant figures (significant digits) displayed
* Format numbers with “,” and ” ” like this 123,456.789 123
* Align columns on decimal points–this is best for readability
* Fit the columns automatically based on data and formatting

There are many code snippets that do one part or another of this, but 
I didn’t find anything that fulfilled all the requirements. So 
I created couple of simple classes to accomplish the task. One 
caveat is that the output needs to be displayed in a constant-space font.


CSV output is the default behavior.  Use -l to create LaTeX output.

Examples
========

Custom s.f. per value

      column 1       |        column 2         |       column C    |    another column    
----------------------------------------------------------------------------------------- 
     1,000.          |       1,200.            |      1,230.       |        123.          
       123.2         |       1,000.            |        123.2      |      1,234.          
     1,234.2         |      -1,234.2           |      1,234.23     |      1,234.234       
     1,234.234 00    |           0.2           |          0.23     |         -0.23        
         0.235       |           0.234 6       |          0.234 57 |          0.002 35    
         0.000 234 6 |           0.000 023 457 | 23,000,000.       | 23,460,000.          
23,456,800.          | -23,456,800.            | 23,456,789.       | 23,456,789.123 345 0 
----------------------------------------------------------------------------------------- 

*****
Constant 3 s.f. (csv2table.py uses default 4, e.g. cat test.csv | ./csv2table.py -s3)

      column 1     |        column 2       |       column C |    another column 
------------------------------------------------------------------------------- 
     1,230.        |       1,230.          |      1,230.    |        123.       
       123.        |       1,230.          |        123.    |      1,230.       
     1,230.        |      -1,230.          |      1,230.    |      1,230.       
     1,230.        |           0.235       |          0.235 |         -0.235    
         0.235     |           0.235       |          0.235 |          0.002 35 
         0.000 235 |           0.000 023 5 | 23,500,000.    | 23,500,000.       
23,500,000.        | -23,500,000.          | 23,500,000.    | 23,500,000.       
------------------------------------------------------------------------------- 

*****
latex table output requires siunitx package:


%% Table generated by formatTable utility %%
%% add \usepackage[group-separator={,}]{siunitx}
%%
\begin{table}
\begin{tabular}{|S|S|S|S|}
\hline
   {column 1}    &     {column 2}      &    {column C} & {another column} \\
\hline
    1230.        &      1230.          &     1230.     &      123.        \\
     123.        &      1230.          &      123.     &     1230.        \\
    1230.        &     -1230.          &     1230.     &     1230.        \\
    1230.        &         0.235       &        0.235  &       -0.235     \\
       0.235     &         0.235       &        0.235  &        0.002 35  \\
       0.000 235 &         0.000 023 5 & 23500000.     & 23500000.        \\
23500000.        & -23500000.          & 23500000.     & 23500000.        \\
\hline
\end{tabular}
\end{table}
%% Table generated by formatTable utility %%


For additional features and help, type

> ./csv2table.py -h
Usage: csv2table.py [options]

Options:
  -h, --help            show this help message and exit
  -l, --latex           Generate LaTeX table (uses siunitx).
  -s SF, --sig-figs=SF  Significant figures (default is 4).
  -c SFLIST, --sig-figs-by-column=SFLIST
                        Significant figures by column as list e.g.
                        "[1,2,2,2,4]"



Enjoy!


LICENSE IS: https://creativecommons.org/licenses/by-sa/3.0/us/

About

Python script to create a text-formatted table of numbers that are decimal aligned, display a set number of significant figures and formatted with commas and spaces for thousands and thousanths..

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages