Skip to content

Instantly share code, notes, and snippets.

@moriyoshi
Created April 12, 2012 02:39
Show Gist options
  • Save moriyoshi/2364331 to your computer and use it in GitHub Desktop.
Save moriyoshi/2364331 to your computer and use it in GitHub Desktop.
# encoding: utf-8
import unicodedata
import csv
import sys
wt = { 'W':2, 'A':2 }
encoding = 'UTF-8'
def len_in_eaw(s):
retval = 0
for c in s:
retval += wt.get(unicodedata.east_asian_width(c), 1)
return retval
def main():
rows = []
mlen = []
for cols in csv.reader(sys.stdin):
cols = [unicode(col, encoding) for col in cols]
rows.append(cols)
if len(mlen) < len(cols):
mlen += [0] * (len(cols) - len(mlen))
for i, col in enumerate(cols):
mlen[i] = max(mlen[i], len_in_eaw(col))
for cols in rows:
for i in range(0, len(mlen)):
print '-' * mlen[i],
print
for i in range(0, len(mlen)):
col = cols[i] if i < len(cols) else u''
print (col + (u' ' * (mlen[i] - len_in_eaw(col)))).encode(encoding),
print
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment