-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathREADME
97 lines (74 loc) · 4.2 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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/