常用方法
list交集并集差集处理
交集list(set(a).intersection(set(b)))并集list(set(a).union(set(b)))差集list(set(b).difference(set(a))) # b中有而a中没有的
正则转义
\d #匹配任何十进制数相当于[0-9] \D #匹配任何非数字字符,相当于[^0-9] \s #匹配任何空白字符,相当于[\t\n\r\f\v] \S #匹配任何非空白字符,相当于[^\t\n\r\f\v] \w #匹配任何字母数字字符,相当于[a-zA-Z0-9_] \W #匹配任何非字符数字字符,相当于[^a-zA-Z0-9_]
数据库操作
import MySQLdb#coding:utf-8#mysql>create table mytable (id int , username char(20));conn = MySQLdb.connect(user='root',passwd='admin',host='127.0.0.1')#连接到数据库服务器cur = conn.cursor()#连接到数据库后游标的定义conn.select_db('test')#连接到test数据库cur.execute("insert into mytable(id,username) value(2,'mo');")#插入一条数据sqlim = "insert into mytable(id,username) values(%s,%s);"cur.executemany(sqli,[(4,'haha'),(5,'papa'),(6,'dada')])#使用格式化字符串,一次添加多条数据,同理可应用于修改和删除cur.execute('delete from mytable where id=4')#删除一条数据cur.execute("update mytable set username='gogo' where id=5")#修改一条数据cur.execute("select * from mytable")cur.fetchone()cur.scroll(0,'absolute')cur.fetchmany()#查询一条数据,先select出数据条目数量,再通过fetchone依次取值,取值完成后可以通>>过scroll重新定义游标位置,如上为让游标在到开头,使用getchmany可以以元组形式取出所有值cur.fetchmany(cur.execute("select* from mytable"))#使用这种方法可以直接取出所有值cur.close()#关闭游标conn.close()#关闭数据库连接
安装及配置相关
pip install -i https://pypi.tuna.tsinghua.edu.cn/simpl
源码安装
yum -y install gcc* make* cmake openssl* autoconf libtool libevent-devel pcre pcre-devel zlibwget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgztar -zxvf Python-3.6.8.tgzcd Python-3.6.8./configuremakemake installpython3 -m venv ./py_venvcd py_venvsource bin/activate
导出依赖
virtualenv env1 --no-site-packages#导出requirementspip freeze > requirements.txtpip install -r requirements.txtpip install pipreqspipreqs ./ --encoding=utf8#Logging 模块 https://www.cnblogs.com/CJOKER/p/8295272.htmlpip install -i https://pypi.tuna.tsinghua.edu.cn/simple git
学习文章