File: table.py

package info (click to toggle)
python-numarray 1.5.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,668 kB
  • ctags: 11,384
  • sloc: ansic: 113,864; python: 22,422; makefile: 197; sh: 11
file content (32 lines) | stat: -rw-r--r-- 949 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import sys
import re

def table(filename, caption="", justification="l", line="|"):
    file = open(filename, "r")
    row1 = file.readline()
    columns = len(row1.split(" "))
    file = open(filename, "r")
    print "\\begin{table}[h]"
    print "\\footnotesize"
    print "\\centering"
    print "\\caption{%s}" % caption
    print "\\label{tab:%s}" % "-".join(caption.split())
    if line != None:
        spec = line +((justification+line)*columns)
    else:
        spec = (justification*columns)
    print "\\begin{tabular}{%s}" % spec
    if line != None:
        print "\\hline"
    for l in file.readlines():
        w = l.strip().split(" ")
        if len(w) != columns:
            raise "column count mismatch in line:",l
        print "&".join(w) + "\\\\"
        if line != None:
            print "\\hline"
    print "\\end{tabular}"
    print "\\end{table}"

if __name__ == "__main__":
    table(sys.argv[1], caption = sys.argv[2])