Python基础(三)
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 #打开笔记本
搬砖多年终不得要领,遂载源码看之望得真经。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求