#! /sw/bin/python #http://www.cs.nyu.edu/courses/summer07/G22.2110-001/hw01-py-example.txt import re, sys """Rename this script into "csv2fixed.py", and make it executable. Put the following input data into a file called "operators.csv": operator,precedence,arity,associativity,description or,2,binary,left,Boolean OR and,3,binary,left,Boolean AND not x,4,unary,n/a,Boolean NOT "in, not in",5,binary,left,Membership tests "is, is not",6,binary,left,Identity tests "<, <=, >, >=, <>, !=, ==",7,binary,chained,Comparisons """ if 1 == len(sys.argv): file = sys.stdin elif 2 == len(sys.argv): file = open(sys.argv[1], 'r') else: print 'usage: tables.py ' sys.exit(-1) def read_table(file): table = [ ] while True: line = file.readline() if '' == line: break line = line.strip() match = re.findall(r'[^",][^,]*|"[^"]+"', line) row = [ ] for value in match: if '"' == value[0]: row.append(value[1:-1]) else: row.append(value) table.append(row) return table def write_table(table, file): widths = [ ] for row in table: for i in range(len(row)): while i >= len(widths): widths.append(0) width = len(row[i]) if widths[i] < width: widths[i] = width for row in table: for i in range(len(row)): if 0 != i: file.write(' ') file.write('%-*s' % (widths[i], row[i])) file.write('\n') table = read_table(file) write_table(table, sys.stdout)