• Willkommen im Linux Club - dem deutschsprachigen Supportforum für GNU/Linux. Registriere dich kostenlos, um alle Inhalte zu sehen und Fragen zu stellen.

Skript zum html-Tabellen erzeugen

Hallo,

ich hoffe, ich bin hier richtig. Ich wollte den Inhalt einer Textdatei "test":
Code:
abc def ghi
jkl mno pqr
stu vwx yz
in einer html-Tabelle haben. Von Hand ist das nervig, hab' daher ein kleines Skript geschrieben:
Code:
#!/usr/bin/env python
#-*- coding: iso-8859-1 -*-

# mktable.py

import os
import sys

if len(sys.argv) < 2:
    print """
mktable.py

Usage: mktable.py inputfile [-s {field-separator}]

The default-field-separator is "\\t" (tabulator):
Please don't give the "-s"-option, if you want to use it.

Output is stdout: Please use ">" to redirect it to a file like in:
mktable.py inputfile > out.html
"""
    sys.exit(1)

sep = "\t"

if len(sys.argv) == 4:
    if sys.argv[2] == "-s":
        sep = sys.argv[3] 

try:
    fh = file(sys.argv[1], "r")
    a = fh.readlines()
    fh.close

except IOError:
    print "Error reading input-file."
    sys.exit(1)
    
if len(a) == 0:
    print "Input-file empty. Nothing done."
    sys.exit(1)

b = ["<table border>"]

for i in a:

    i = i.rstrip("\n")

    c = i.split(sep)

    b.append("<tr>")

    for u in c:
        b.append("<td>" + u + "</td>")

    b.append("</tr>")

b.append("</table>")

for i in b:
    print i
mit dem man mit
Code:
mktable.py test -s " " > out.html
die Tabelle bekommt.

HTH anyone ...

Gruß
 
Oben