zoukankan      html  css  js  c++  java
  • Python3 从零单排12_logging&excel&redis&mysql

      logging模块

    import logging
    from logging import  handlers
    
    #1.生成loger 对象 logger = logging.getLogger("web") #日志级别过滤顺序:全局-->haddler 全局默认是warning,那么不设置的情况下控制台和文件只能在warning级别的基础上进行设置,不能输出info和debug # logger.setLevel(logging.INFO) #1.1 filter加进logger class IgnoreShit(logging.Filter): def filter(self,record): return "shit" not in record.msg #过滤掉含有shit的日志 logger.addFilter(IgnoreShit()) #2.生成haddler 对象 ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) # fh = logging.FileHandler("ch_fg_test.log") #当日志文件达到10bytes的时候,之前的日志重命名,最多存在3个,超过3个删除 fh = handlers.RotatingFileHandler("web.log",maxBytes=10,backupCount=3) #每5秒生成一个新的日志文件,之前的日志重命名,最多存在3个,超过3个删除 fh2 = handlers.TimedRotatingFileHandler("web.log",when="S",interval=5,backupCount=3) fh.setLevel(logging.DEBUG) #2.1.生成handler绑定loger 对象 logger.addHandler(ch) logger.addHandler(fh) #3.生成formatter 对象 file_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') console_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(lineno)d - %(message)s') #3.1.生成formatter绑定filter 对象 ch.setFormatter(console_formatter) fh.setFormatter(file_formatter) logger.debug("test debug") logger.info("test info") logger.warning("test warning") logger.error("test error") logger.error("test shit error")

      excel模块

    import xlrd,xlwt,xlutils,os
    from xlutils.copy import copy
    
    
    book=xlrd.open_workbook('stu.xls')#打开一个excel文件对象
    sheet=book.sheet_by_name('Sheet1')#通过sheet 名称指定工作sheet
    # sheet=book.sheet_by_index(0)#通过sheet 索引指定工作sheet
    all_sheets=book.sheet_names()#获取所有sheet名称,返回一个list
    print(all_sheets)
    print(sheet.cell(0,0).value)#通过cell获取指定坐标的数据
    print(sheet.nrows)#获取sheet页的行数
    print(sheet.ncols)#获取sheet页的列数
    print(sheet.row_values(0))#获取指定行数的数据
    print(sheet.col_values(0))#获取指定列数的数据
    
    workbook=xlwt.Workbook()#打开一个excel文件对象
    wsheet=workbook.add_sheet('sheet1')#添加一个sheet
    wsheet.write(0,0,'test')#写入数据
    workbook.save('test.xls')#保存excel,后缀必须是.xls,否则报错
    
    ubook=xlrd.open_workbook('stu.xls')#打开一个excel文件对象
    mbook=copy(ubook)#复制读到的文件对象
    msheet=mbook.get_sheet(0)#获取sheet页,注意这里只能用get_sheet(0)方法,指定下标
    msheet.write(0,0,'new_data')#写入数据
    mbook.save('new.xls')#保存数据
    os.remove('stu.xls')
    os.rename('new.xls','stu.xls')

      

      redis模块

    import redis

    coon
    =redis.Redis(host='192.168.217.128',password='123456',port=6379,db=1) print(coon.hget('Idcards','花校纳').decode())#decode转码,这里get到的数据是bytes格式的,需要转码 coon.set('葛怕愿',123456789) print(coon.get('戴铁视').decode()) coon.hset('Idcards','牛逼',123456789) print(coon.hget('Idcards','牛逼').decode()) print(coon.keys())#数据库上所有的key res=coon.hgetall('Idcards') for i in res: print(i.decode(),':',res[i].decode()) coon.delete('yxg')#删除数据,yxg是key # 需求是把一个redis库里面所有的数据,导入到另一个redis里面 # 1、r1 和r2 连接上r1和r2两个数据库 # 2、 获取到r1上面所有的key keys # 3、判断key的类型,r.type(k),get hgetall .hset() .set() r1=redis.Redis(host='211.149.218.16',port=6378,password='123456',db=2)#获取数据 r2=redis.Redis(host='211.149.218.16',port=6378,password='123456',db=3)#写数据 keys = r1.keys()#r1数据库上所有的key for k in keys: if r1.type(k)==b'hash':#判断是否为哈希类型,因为redis里面返回的数据都是bytes类型的, #所以在hash前面加上b hash_data = r1.hgetall(k)#获取哈希的类型的数据 for k2,v in hash_data.items():#循环刚才获取到的字典 r2.hset(k,k2,v)#set哈希类型的值 else: v = r1.get(k)#从r1里面获取值, r2.set(k,v)#set进去

      mysql模块

    import pymysql
    
    
    from pymysql.cursors import DictCursor
    coon=pymysql.connect(host='localhost',port=3306,user='root',password='root',db='test',charset='utf8')
    cur=coon.cursor(DictCursor)
    cur.execute('select * from stu')
    res=cur.fetchall()
    print(res)
    for i in res:
        print(i)
    cur.close()
    coon.close()
  • 相关阅读:
    错误: error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. 的处理方法
    C语言习题
    嵌入式芯片STM32F407
    c语言课后习题
    求方程式的根
    C语言课后习题
    LINUX常用指令
    在 pythonanywhere 上搭建 django 程序(Virtualenv+python2.7+django1.8)
    Git远程操作详解
    ./configure,make,make install的作用
  • 原文地址:https://www.cnblogs.com/znyyy/p/10072817.html
Copyright © 2011-2022 走看看