• 将pgadmin导出的数据转换成INSERT语句


    #!/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:]) 
  • 相关阅读:
    Element-ui 的 slot 关系理解
    关于Delegate委托和Event事件的学习
    JavaScript 中 prototype 与 __proto__
    正向代理与反向代理的个人理解
    MVC和三层架构
    关于SqlDataAdapter的思考
    关于C#连接Oracle数据库
    关于VS配置环境
    富文本的实现
    博客
  • 原文地址:https://www.cnblogs.com/Mingxx/p/3041490.html
Copyright © 2020-2023  润新知