Python基础(三)

正则表达式
import re
#match 从开头位置匹配,失败返回NULL
result = re.match("^[a-z0-9]*@(sina|qq|126|136)\.com$","112@qq.com")
print("true","match string:",result.group(0))
# search 搜索满足条件的字符串
result = re.search("[0-9]+","asasas123123cacs")
print(result.group())
# findall 查找所有满足条件的字符串
result = re.findall("[0-9]+","asasas123ca123cs")
print(result)
# sub 查找替换所有满足条件的字符串
result = re.sub("[0-9]+","1111","asasas2222cass")
print(result)
# split 按照正则表法是拆分字符串
result = re.split("[0-9]+","as111asas2222aaac3333ass")
print(result)#['as', 'asas', 'aaac', 'ass']
爬虫
import urllib.request
import re
urlobj = urllib.request.urlopen(url="https://www.ygdy8.net/html/gndy/dyzz/index.html")
html = urlobj.read().decode("GBK")
#print(html)
result_list = re.findall("a href=\".*\" class",html)
print(len(result_list))
for film in result_list:
   print("https://www.ygdy8.net"+film[8:43])
装饰器
def function_out(func):
   def function_in():
       func()
   return function_in()
def login():
   print("login")
function_out(login)
GIL全局解释器锁
#GIL全局解释器锁
#释放的时间:线程执行完毕、IO阻塞、执行时间达到阈值
#GIL全局解决方案:多进程替换多线线程、子线程部分使用C语言写
拷贝
#python的可变和不可变
#可变:字典、列表    不可变:数字、字符串、元组(当元素修改,重新分配空间)

#浅拷贝/不可变类型=新对象和原始对象一致
#浅拷贝/可变类型 =简单对象(新对象和原始对象不同) 复杂对象(对象改变会跟着改变)
#深拷贝/不可变类型=新对象和原始对象对立
import copy
#copy浅拷贝
list=[1,2,3]#可变
list_copy=copy.copy(list)      #可变类型浅拷贝也会产生新空间
#list_copy=copy.deepcopy(list)#可变类型深拷贝也会产生新空间
print(id(list_copy))
print(id(list))
模块
import sys
sys.path.insert(0,"/data/aa.py"#追加到开头
sys.path.append("/data/aa.py"#追加到末尾
import importlib
#import 具有防止重复包含的作用
importlib.reload("/data/aa.py")
多继承
#allbase/pigbase/pig/dogbase/dog/animal
class allbase():
   def __init__(self):
       print("allbase")
class dogbase(allbase):
   def __init__(self):
       super().__init__()
       print("dogbase")
class pigbase(allbase):
   def __init__(self):
       super().__init__()
       print("pigbase")
class my(dogbase,pigbase):
   def __init__(self):
       super().__init__()
       print("my")
class dog(dogbase):
   def __init__(self):
       super().__init__()
       print("dog")
class pig(pigbase):
   def __init__(self):
       super().__init__()
       print("pig")
class animal(dog,pig):
   def __init__(self):
       super().__init__()
       print("animal")
an=animal()
print(type(an).__mro__)
property
#property 像属性一样使用变量、方法
class PageInfo:
   def __init__(self,num):
       self.num = num
   @property
   def start(self):
       return self.num*10
   @property
   def end(self):
       return (self.num-1)*10
   def get(self):
       return "aa"
   def set(self,value):
       self.num=value
   pro = property(get,set)#第一个参数获取属性的方法,第二个参数设置属性的方法
pageinfo = PageInfo(4)
#print(pageinfo.start) #调用方法
#print(pageinfo.end)   #调用方法
print(pageinfo.pro)
pageinfo.pro=100
print(pageinfo.num)
with管理上下文
#with管理上下文 方式一
#类必须有 __enter__ __exit__ 方法
class OperFile():
   def __enter__(self):
       self.file = open("D:\data\\aa.txt")
       print("__enter__")
       return self.file
   def __exit__(self, exc_type, exc_val, exc_tb):
       self.file.close()
       print("__exit__")
   def __init__(self,name):
       self.name=name
if __name__ == '__main__':
   with OperFile("name"as file:
       print(file.read())

#with管理上下文 方式二
from contextlib import contextmanager
@contextmanager
def myopen():
   file = open("D:\data\\aa.txt")
   yield file
   file.close()
if __name__ == '__main__':
   with myopen() as mo:
       print(mo.read())
mython操作mysql
#pip install pymysql
import pymysql
db = pymysql.connect(host = '114.116.44.117',
                    user = 'root',password = '123456',db = 'test',charset = 'utf8')
cursor = db.cursor()
try:
   #查询
   #cursor.execute("select * from student")
   #for index in cursor.fetchall():
   #    print(index)
   #插入
   #cursor.execute("insert into student values(1,'zhangsan',1),(2,'lisi',2)")
   #db.commit()
   #修改
   #cursor.execute("update student set name='wangwu' where id=1")
   #db.commit()
   #删除
   cursor.execute("delete from student where id=1")
   db.commit()
except:
   db.rollback()
finally:
   cursor.close()
   db.close()
安装 jupyter
#pip install jupyter
#jupyter notebook  #打开笔记本

原文地址:http://www.cnblogs.com/wuxiaolong4/p/16927403.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性