zoukankan      html  css  js  c++  java
  • python笔记

    最近由于工作需要写了点python小程序,觉得有必要做一点笔记

    一。使用pymssql

    1.引入pymssql包

    import pymssql

    2.建立连接:

    conn = pymssql.connect(server=conf['db_address'],
    port="1433",user=conf['db_username'],
    password=conf['db_password'],
    database=conf['db_name'],

    charset="UTF-8")
    cursor = conn.cursor()

    3.执行SQL语句:

    cursor.execute(sql)

    4.获得数据:
    search_table = cursor.fetchall()

    5.修改数据:

    cursor.execute(sql,(str(datetime.today()),))

    conn.commit()

    二。定义一个python的类

    #tool class to write data into file
    class OutFile:
        dir_ = ''
        sew_ = '=='
        def __init__(self, dir, sew = '=='):
            self.dir_ = dir
            self.sew_ = sew
        def write_line(self, line, date):
            fp = open(self.dir_ + "/" + date, 'ab')
            print line
            fp.write(line)
            
            fp.write('
    ')
            fp.close()
        def write_vector(self, vec, date):
            fp = open(self.dir_ + "/" + date, 'ab')
            if len(vec) > 0:
                for i in range(0, len(vec) - 1):
                    fp.write(vec[i])
                    fp.write(self.sew_)
                fp.write(vec[len(vec) - 1])
                fp.write('
    ')
            fp.close()
        def write_dict(self, dict, date):
            fp = open(self.dir_ + "/" + date, 'ab')
            for key in dict:
                fp.write(key)
                fp.write(' : ')
                leng = len(dict[key])
                for i in range(0, leng - 1):
                    fp.write(dict[key][i])
                    fp.write(self.sew_)
                fp.write(dict[key][leng - 1])
                fp.write('
    ')
            fp.close()
        def write_dict_dict(self, dict_dict, date):
            fp = open(self.dir_ + "/" + date, 'ab')
            for key in dict_dict:
                fp.write(key.encode('latin-1'))
                print key.encode('latin-1')
                #fp.write(key.decode('utf-8').encode('gbk'))
                dict = dict_dict[key]
                fp.write(self.sew_)
                for key_word in dict:
                    #fp.write(key_word.decode('utf-8').encode('gb2312'))
                    fp.write(key_word.encode('latin-1'))
                    #fp.write(key_word.decode('utf-8').encode('gbk'))
                    fp.write(':')
                    fp.write(str(dict[key_word]))
                    fp.write(self.sew_)
                fp.write('
    ')
            fp.close()
            
    outfile = OutFile('report', '	')
  • 相关阅读:
    HDU 3697贪心
    HDU 3226 背包
    numpy_2nd 新建矩阵的五种方法 array zeros empty arange().reshape()
    numpy_1st 属性 ndim,shape,size
    CV学习笔记第二课(上)
    33. 搜索旋转排序数组 二分法
    35. 搜索插入位置 今天就是二分法专场
    34.在排序数组中查找元素的第一个和最后一个位置 二分法
    CV第三课
    CV第二课(下)
  • 原文地址:https://www.cnblogs.com/candycloud/p/3861136.html
Copyright © 2011-2022 走看看