Commit e2939f47 authored by Kirill Smelkov's avatar Kirill Smelkov Committed by Kirill Smelkov

mdb-astextplain: выравниваем колонки по горизонтали

parent 560066f5
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""pretty-print a csv input, aligning columns"""
import sys
SEP = '|' # XXX '|' separator hardcoded
ENC = 'utf-8' # XXX utf-8 encoding hardcoded
def main():
rows = []
maxw = []
# scan input, prettify fields, collect max coloumns width
for line in sys.stdin.readlines():
line = line.rstrip() # strip trailing '\n'
line = line.decode(ENC) # utf-8 -> unicode
fields = line.split(SEP)
# numbers are pretty-printed
for i, f in enumerate(fields):
try:
fields[i] = int(f)
continue
except ValueError:
pass
try:
fields[i] = float(f)
except ValueError:
pass
if len(fields) > len(maxw):
maxw.extend( [0] * (len(fields) - len(maxw)) )
for i, f in enumerate(fields):
if len(unicode(f)) > maxw[i]:
maxw[i] = len(unicode(f))
rows.append(fields)
# re-output rows, aligning them
for fields in rows:
line = ''
for f, w in zip(fields, maxw):
# numbers are right-aligned, everything else - left
if isinstance(f, (int,long,float)):
fstr = ('%%%is' % w) % f
else:
fstr = ('%%-%is' % w) % f
line = SEP.join((line,fstr))
# strip traling empty field, if any
line = line.rstrip()
print line.encode(ENC)
if __name__ == '__main__':
main()
...@@ -12,11 +12,14 @@ while read ; do ...@@ -12,11 +12,14 @@ while read ; do
tab="$REPLY" tab="$REPLY"
mdb-schema -T "$tab" "$mdbfile" | grep -v '^--' mdb-schema -T "$tab" "$mdbfile" | grep -v '^--'
echo "----8<----" echo "----8<----"
(
# header # header
mdb-export -Q -d '|' "$mdbfile" "$tab" | \ mdb-export -Q -d '|' "$mdbfile" "$tab" | \
head -1 head -1
# content sorted by first column # content sorted by first column
mdb-export -Q -d '|' -H "$mdbfile" "$tab" | \ mdb-export -Q -d '|' -H "$mdbfile" "$tab" | \
sort -t '|' -g sort -t '|' -g
) |
csv-pprint
echo "----8<----" echo "----8<----"
done < $tabfile done < $tabfile
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment