Skip to content

Commit

Permalink
Add alignment options including a smart one
Browse files Browse the repository at this point in the history
  • Loading branch information
dnkats committed Jun 8, 2018
1 parent 80da87e commit 7eb99b1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ Usage:

`-do` : define delimiter for the output

By default the alignment is set to be right unless the entry starts with `=` or `"` (otherwise some programs don't recognize the equations or text fields).
It can be changed with following options:

`-l` : align all entries left

`-r` : align all entries right

`-x` : compress the csv again, i.e., remove the fixed-format formating

Example:

Input file `input.csv`:
Expand Down
26 changes: 25 additions & 1 deletion csvfwf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
delimiter = ","
#default delimiter for input files
inpdelim = ","
#smart alignment by default
ALIGN = 0
#compress the table again
COMPRESS = False

def GetTable(CSVFile):
"""read csv to a table"""
Expand Down Expand Up @@ -89,7 +93,18 @@ def FixWidth(vals):
else:
rowwidth = 0
offsrow[irow] -= maxlen - rowwidth
fcol.append('{:>{}}'.format(vals[irow][icol],rowwidth))
alignright = True
if ALIGN is -1:
alignright = False
elif ALIGN is 0:
if vals[irow][icol].startswith('"') or vals[irow][icol].startswith('='):
alignright = False
if COMPRESS:
fcol.append(vals[irow][icol])
elif alignright:
fcol.append('{:>{}}'.format(vals[irow][icol],rowwidth))
else:
fcol.append('{:{}}'.format(vals[irow][icol],rowwidth))
itlen = len(vals[irow][icol])
if itlen > maxlen:
offsrow[irow] = itlen - maxlen;
Expand All @@ -114,6 +129,15 @@ def PrintTable(table):
# input delimiter
Arg = next(ArgsLoop)
inpdelim = Arg
elif Arg.startswith("-l"):
# align left
ALIGN = -1
elif Arg.startswith("-r"):
# align right
ALIGN = 1
elif Arg.startswith("-x"):
# make it ugly again
COMPRESS = True
else:
print("option "+Arg+" not known")
else:
Expand Down

0 comments on commit 7eb99b1

Please sign in to comment.