#!/usr/bin/python # -*- coding: iso-8859-1 -*- # (c) Thomas Guettler http://www.thomas-guettler.de/ # This script is in the public domain # Convert a csv file to Wiki Syntax (http://moinmoin.wikiwikiweb.de/HelpOnTables) # Python Imports import os import re import csv import sys def usage(): print """Usage: %s file.csv [file.wiki] Read file.csv and create file.wiki. If you use "-" as second argument, the wiki file will be written to stdout. """ % ( os.path.basename(sys.argv[0])) def main(): if len(sys.argv) not in [2, 3]: usage() sys.exit() csvfile=sys.argv[1] if not os.path.isfile(csvfile): print "%s is not a file" % csvfile sys.exit(3) if len(sys.argv)==3: wikifile=sys.argv[2] else: match=re.match(r'^(.*)\.(.*)$', csvfile) if match: wikifile="%s.wiki" % match.group(1) else: wikifile="%s.wiki" % csvfile if wikifile=="-": fdout=sys.stdout else: fdout=open(wikifile, "wt") sniff=open(csvfile).read(1024) dialect=csv.Sniffer().sniff(sniff) fdin=open(csvfile) reader=csv.reader(fdin, dialect) for row in reader: fdout.write("||") fdout.write("||".join(row)) fdout.write("||\n") fdout.close() if __name__=="__main__": main()