@@ -32,6 +32,7 @@ def _is_file(f):
32
32
PRESERVE_WHITESPACE = False
33
33
34
34
_DEFAULT_FLOATFMT = "g"
35
+ _DEFAULT_INTFMT = ""
35
36
_DEFAULT_MISSINGVAL = ""
36
37
# default align will be overwritten by "left", "center" or "decimal"
37
38
# depending on the formatter
@@ -1032,7 +1033,7 @@ def _column_type(strings, has_invisible=True, numparse=True):
1032
1033
return reduce (_more_generic , types , bool )
1033
1034
1034
1035
1035
- def _format (val , valtype , floatfmt , missingval = "" , has_invisible = True ):
1036
+ def _format (val , valtype , floatfmt , intfmt , missingval = "" , has_invisible = True ):
1036
1037
"""Format a value according to its type.
1037
1038
1038
1039
Unicode is supported:
@@ -1047,8 +1048,10 @@ def _format(val, valtype, floatfmt, missingval="", has_invisible=True):
1047
1048
if val is None :
1048
1049
return missingval
1049
1050
1050
- if valtype in [ int , str ] :
1051
+ if valtype is str :
1051
1052
return f"{ val } "
1053
+ elif valtype is int :
1054
+ return format (val , intfmt )
1052
1055
elif valtype is bytes :
1053
1056
try :
1054
1057
return str (val , "ascii" )
@@ -1324,6 +1327,7 @@ def tabulate(
1324
1327
headers = (),
1325
1328
tablefmt = "simple" ,
1326
1329
floatfmt = _DEFAULT_FLOATFMT ,
1330
+ intfmt = _DEFAULT_INTFMT ,
1327
1331
numalign = _DEFAULT_ALIGN ,
1328
1332
stralign = _DEFAULT_ALIGN ,
1329
1333
missingval = _DEFAULT_MISSINGVAL ,
@@ -1398,6 +1402,10 @@ def tabulate(
1398
1402
Table formats
1399
1403
-------------
1400
1404
1405
+ `intfmt` is a format specification used for columns which
1406
+ contain numeric data without a decimal point. This can also be
1407
+ a list or tuple of format strings, one per column.
1408
+
1401
1409
`floatfmt` is a format specification used for columns which
1402
1410
contain numeric data with a decimal point. This can also be
1403
1411
a list or tuple of format strings, one per column.
@@ -1843,15 +1851,25 @@ def tabulate(
1843
1851
float_formats = list (floatfmt )
1844
1852
if len (float_formats ) < len (cols ):
1845
1853
float_formats .extend ((len (cols ) - len (float_formats )) * [_DEFAULT_FLOATFMT ])
1854
+ if isinstance (intfmt , str ): # old version
1855
+ int_formats = len (cols ) * [
1856
+ intfmt
1857
+ ] # just duplicate the string to use in each column
1858
+ else : # if intfmt is list, tuple etc we have one per column
1859
+ int_formats = list (intfmt )
1860
+ if len (int_formats ) < len (cols ):
1861
+ int_formats .extend ((len (cols ) - len (int_formats )) * [_DEFAULT_INTFMT ])
1846
1862
if isinstance (missingval , str ):
1847
1863
missing_vals = len (cols ) * [missingval ]
1848
1864
else :
1849
1865
missing_vals = list (missingval )
1850
1866
if len (missing_vals ) < len (cols ):
1851
1867
missing_vals .extend ((len (cols ) - len (missing_vals )) * [_DEFAULT_MISSINGVAL ])
1852
1868
cols = [
1853
- [_format (v , ct , fl_fmt , miss_v , has_invisible ) for v in c ]
1854
- for c , ct , fl_fmt , miss_v in zip (cols , coltypes , float_formats , missing_vals )
1869
+ [_format (v , ct , fl_fmt , int_fmt , miss_v , has_invisible ) for v in c ]
1870
+ for c , ct , fl_fmt , int_fmt , miss_v in zip (
1871
+ cols , coltypes , float_formats , int_formats , missing_vals
1872
+ )
1855
1873
]
1856
1874
1857
1875
# align columns
@@ -2266,6 +2284,7 @@ def _main():
2266
2284
-o FILE, --output FILE print table to FILE (default: stdout)
2267
2285
-s REGEXP, --sep REGEXP use a custom column separator (default: whitespace)
2268
2286
-F FPFMT, --float FPFMT floating point number format (default: g)
2287
+ -I INTFMT, --int INTFMT integer point number format (default: "")
2269
2288
-f FMT, --format FMT set output table format; supported formats:
2270
2289
plain, simple, grid, fancy_grid, pipe, orgtbl,
2271
2290
rst, mediawiki, html, latex, latex_raw,
@@ -2281,14 +2300,15 @@ def _main():
2281
2300
opts , args = getopt .getopt (
2282
2301
sys .argv [1 :],
2283
2302
"h1o:s:F:A:f:" ,
2284
- ["help" , "header" , "output" , "sep=" , "float=" , "align=" , "format=" ],
2303
+ ["help" , "header" , "output" , "sep=" , "float=" , "int=" , " align=" , "format=" ],
2285
2304
)
2286
2305
except getopt .GetoptError as e :
2287
2306
print (e )
2288
2307
print (usage )
2289
2308
sys .exit (2 )
2290
2309
headers = []
2291
2310
floatfmt = _DEFAULT_FLOATFMT
2311
+ intfmt = _DEFAULT_INTFMT
2292
2312
colalign = None
2293
2313
tablefmt = "simple"
2294
2314
sep = r"\s+"
@@ -2300,6 +2320,8 @@ def _main():
2300
2320
outfile = value
2301
2321
elif opt in ["-F" , "--float" ]:
2302
2322
floatfmt = value
2323
+ elif opt in ["-I" , "--int" ]:
2324
+ intfmt = value
2303
2325
elif opt in ["-C" , "--colalign" ]:
2304
2326
colalign = value .split ()
2305
2327
elif opt in ["-f" , "--format" ]:
@@ -2325,6 +2347,7 @@ def _main():
2325
2347
tablefmt = tablefmt ,
2326
2348
sep = sep ,
2327
2349
floatfmt = floatfmt ,
2350
+ intfmt = intfmt ,
2328
2351
file = out ,
2329
2352
colalign = colalign ,
2330
2353
)
@@ -2336,16 +2359,17 @@ def _main():
2336
2359
tablefmt = tablefmt ,
2337
2360
sep = sep ,
2338
2361
floatfmt = floatfmt ,
2362
+ intfmt = intfmt ,
2339
2363
file = out ,
2340
2364
colalign = colalign ,
2341
2365
)
2342
2366
2343
2367
2344
- def _pprint_file (fobject , headers , tablefmt , sep , floatfmt , file , colalign ):
2368
+ def _pprint_file (fobject , headers , tablefmt , sep , floatfmt , intfmt , file , colalign ):
2345
2369
rows = fobject .readlines ()
2346
2370
table = [re .split (sep , r .rstrip ()) for r in rows if r .strip ()]
2347
2371
print (
2348
- tabulate (table , headers , tablefmt , floatfmt = floatfmt , colalign = colalign ),
2372
+ tabulate (table , headers , tablefmt , floatfmt = floatfmt , intfmt = intfmt , colalign = colalign ),
2349
2373
file = file ,
2350
2374
)
2351
2375
0 commit comments