内置函数
res = any([1,1,0]) #如果这个list里面有一个为真,就返回true print(res) res = all([1,1,1,1,1]) #如果这个list里面全部为真,就返回true print(res)
import string case = list(string.ascii_uppercase) for index,c in enumerate(case): print('%s => %s'%(c,index))
import xlwt book = xlwt.Workbook() sheet = book.add_sheet('xuesheng') all_data = [ ['id','name','sex','phone','country'], [1,'xiaoming','男',123423432,'china'], ['2','xiaoming','男',123423432,'china'], ['3','xiaoming','男',123423432,'china'], ['4','xiaoming','男',123423432,'china'], ['5','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ['6','xiaoming','男',123423432,'china'], ] # line = 0 #行号 # for data in all_data: #控制行,每次循环行都会变 # # ['id','name','sex','phone','country'] # #[1, 'xiaoming', '男', 123423432, 'china'], # col = 0 # 列 # for d in data: #控制列,每次循环lie都会变 # # id name sex # sheet.write(line,col,d) # col+=1 # line+=1
for line,data in enumerate(all_data): for col,d in enumerate(data): sheet.write(line, col, d) book.save('s2.xls')
Redis操作
import redis ip = '118.24.3.40' password='HK139bc&*' r = redis.Redis(host=ip,password=password,port=6379,db=1, decode_responses=True) # decode_responses这个参数的意思是,返回的二进制数据直接decode一下 print(r.get('python:lyos')) print(r.hgetall('jnz_stus')) r.flushdb() #删除这个数据库里面所有的key #string res = r.get('nhy2') r.set('nhy','acb123',24*60*60) #新增和修改都是它,后面为刷新时间 r.delete('nhy') #删除指定的key r.set('python:os','listdir,path') res = r.get('python:os') print(res) print(r.keys('session*')) #获取所有的key
import redis #哈希类型 hash类型 ip = '118.24.3.40' password='HK139bc&*' #二层字典 r = redis.Redis(host=ip,password=password,port=6379,db=1, decode_responses=True) # decode_responses这个参数的意思是,返回的二进制数据直接decode一下 print(r.get('python:lyos')) print(r.hgetall('jnz_stus')) r.hset('jnz_stus','yangfan','sdfsdfsdfsdf') res = r.hget('jnz_stus','cm') #指定获取里面小key的值 r.delete('jnz_stus') #删除大key r.hdel('jnz_stus','cm') #删除指定的小key res = r.hgetall('jnz_stus') #获取到大key里面所有的数据 print(res) new = {} for k,v in res.items(): new [k.decode()] = v.decode() print('======下面是转完之后的') print(new) # 1、先循环res # 2、k和v decode一下 然后放到new这个字典里面 session = { "nhy":{'sex':18,'age':18}, "nhy2":{'sex':18,'age':18}, } token = { "nhy2":{'x':'x'} }
s='水电费水电费' #JP231 s2= s.encode('JP231') #先按照之前的字符集解码 s2.decode('utf-8') #然后再编码,编成你要的字符集 #两种不同类型的字符集要互相转的话,先转成unicode
发送邮件
import yagmail user='123@163.com' password='123' m = yagmail.SMTP(host='smtp.163.com',user=user, password=password, ) #smtp.qq.com #smtp_ssl=True 如果是qq邮箱的话加这个参数 m.send(to=['111@qq.com','222@163.com'], cc=['333@qq.com','444@163.com'], subject='明天不上课',contents='明天不上课,在家好好休息。。。', attachments=['笔记.txt',r'C:Users hyDesktop金牛座.xls'])
写日志
# error # waring # info # debug import nnlog log = nnlog.Logger('my.log',level='info',backCount=5,when='D') log.info('xiaojun登陆...') log.error('数据连接失败!') log.surprise()
模块
#start.py from lib.main import login login()
#settings.py redis_host = '192.168.1.1' redis_port = 6379 redis_db = 1 mysql_host = '118.23.4.40' port = 3306
#main.py from lib.tools import mydb,my_redis def login(): print('登陆') def reg(): print('注册') my_redis()
#tools.py import redis import pymysql from conf import settings def mydb(sql): print('操作数据库') pymysql.connect(host=settings.mysql_host) pass def my_redis(): print('操作redis') r = redis.Redis(host=settings.redis_host,port=settings.redis_port) pass def my_md5(): pass