#!/usr/bin/python import os import sys import re import getopt _SPLITE_ = ';' table_name = '' def parserFile(path): try: f = open(path, 'r') except: print "open [%s] failed." % path return lines = f.readlines() f.close() if len(lines) <= 0: print "no data in file" headers = lines[0].strip("\n").split(_SPLITE_) lines.pop(0) datas = [] items = len(headers) for l in lines: tmp = l.strip('\n').split(_SPLITE_) datas.append(tmp) try: f = open(path + ".sql", 'w') except: print "open [%s] failed." % path return for data in datas: insert_string_header = "INSERT %s(%s) VALUES(%s);\n" % (table_name, ','.join(headers), ','.join(data)) f.write(insert_string_header) f.close() def main(argv): global table_name try: opts, args = getopt.getopt(argv, 't:',['table-name=']) except getopt.GetoptError: print "paramter error" return for o, a in opts: if o in ('-t', '--table-name'): table_name = a print a else: print "unknown paramter %s" % a for a in args: parserFile(a) if __name__ == '__main__': main(sys.argv[1:])