一、基础学习
1.1 输入功能
1.2 占位符
1.3 随机数选择
1.4 for循环使用及乘法表
1.5 文件的操作
from xml.etree import ElementTree as ET
root = ET.Element("home")
son1 = ET.Element('son', {'name': '儿1'})
son2 = ET.Element('son', {'name': '儿2'})
grandson1 = ET.Element('grandson', {'name': '儿11'})
grandson2 = ET.Element('grandson', {'name': '儿12'})
son1.append(grandson1)
son2.append(grandson2)
root.append(son1)
root.append(son2)
tree = ET.ElementTree(root)
tree.write('file/oo.xml', encoding='utf-8', short_empty_elements=tree)
1.6 解析xml文件
content = """<xml>
<ToUserName><![CDATA[gh_7f0B8578]]></ToUserName>
<Status><![CDATA[gh_7f0B8578]]></Status>
</xml>"""
from xml.etree import ElementTree as ET
info = {}
root = ET.XML(content)
for node in root:
info[node.tag] = node.text
print(info)
1.7 操作Excel
from openpyxl import load_workbook
wb = load_workbook("files/p1.xlsx")
1.7.1 单元格样式(边框)
from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFill
wb = load_workbook("files/p1.xlsx")
sheet = wb.worksheets[0]
cell = sheet.cell(9, 2)
cell.alignment = Alignment(horizontal='center', vertical='center', text_rotation=0, wrap_text=True)
cell.border = Border(
top=Side(style="thin", color="FFB6C1"),
bottom=Side(style="dashed", color="FFB6C1"),
left=Side(style="dashed", color="FFB6C1"),
right=Side(style="dashed", color="9932CC"),
diagonal=Side(style="thin", color="483D88"),
diagonalUp=True,
diagonalDown=True
)
wb.save("p2.xlsx")
1.7.2 crud操作
sheet.delete_rows(idx=1, amount=2)
sheet.delete_cols(idx=1, amount=3)
wb.save("p2.xlsx")
1.7.3 打印操作
from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFill
wb = load_workbook("files/p1.xlsx")
sheet = wb.worksheets[0]
sheet.print_area = "A1:D200"
sheet.print_title_cols = "A:D"
sheet.print_title_rows = "1:3"
wb.save("files/p2xlsx")
1.8 发送邮件
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
msg = MIMEText("约吗", 'html', 'utf-8')
msg['From'] = formataddr(["haha", "yangyan@126.com"])
msg['Subject'] = "180一晚"
server = smtplib.SMTP_SSL("smtp.126.com")
server.login("yangyan@126.com", "LADFKNJHFD")
server.sendmail("发送者邮箱", "接收者邮箱", msg.as_string())
server.quit()
二、函数使用
2.1 基本函数
def fucn(*args, **kwargs):
print(args,kwargs)
fucn(11, 12 )
fucn(11, 12, name="haha", age=19)
fucn([11, 12, 33], {"k1": 1, "k2": 13})
(11, 12) {}
(11, 12) {'name': 'haha', 'age': 19}
([11, 12, 33], {'k1': 1, 'k2': 13}) {}
(11, 12, 33) {'k1': 1, 'k2': 13}
2.2 匿名函数
基于lambda定义的函数格式为:lambda 参数:函数体
lambda x:函数体
lambda x,y:函数体
lambda *args, **kwargs:函数体
fucn = lambda x: x+10
v1 = fucn(10)
print(v1)
三元运算
num = input("请输入名字:")
data = "臭不要脸" if "苍老师" in num else "正经人"
print(data)
2.3 生成器
import random
def gen_random_num(max_count):
counter = 0
while counter < max_count:
yield random.randint(1000, 9999)
counter += 1
data_list = gen_random_num(300000000)
2,4 模块导包
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
2.5 文件注释
2.6 json格式转换
import json
data_dic = json.load(re.text)
import json
data_dic = json.dumps(data)
import json
from decimal import Decimal
from datetime import datetime
data = [
{"id": 1, "name": "heh", 'size': Decimal("18.99"), 'ctime': datetime.now()},
{"id": 2, "name": "www", 'size': Decimal("9.99"), 'ctime': datetime.now()}
]
class MyJSONEncoder(json.JSONEncoder):
def default(self, o):
if type(o) == Decimal:
return str(o)
elif type(o) == datetime:
return o.strftime("%Y-%M-%d")
return super().default(o)
res = json.dumps(data, cls=MyJSONEncoder)
print(res)
2.7 正则表达式
import re
text = "拉欧著牛逼, 在线要333232332.@qq.com, 手机号是13829323293"
phone_list = re.findall("1[3|5|8|9]\d{9}", text)
print(phone_list)
phone_list = re.findall("[abc]", text)
phone_list = re.findall("q[abc]", text)
phone_list = re.findall("[^abc]", text)
三、对象的了解
3.1 简单对象了解
class Message:
def send_email(self, email, content):
data = "给{}发邮件, 内容是: {}".format(email, content)
print(data)
msg_object = Message()
msg_object.send_email("hehhhe.com", "注册成功")
3.2 三大方法
封装, 继承, 多态
继承:(支持多继承)
class Father:
def send_email(self):
print("我是父类")
class Son(Father):
def inses_email(self):
print("我是子类")
son_objiect = Son()
son_objiect.send_email()
son_objiect.inses_email()
多态:含有send()方法
静态方法: 在函数上添加 :@staticmethod
绑定方法: 函数中默认有 def send(self):
3.3 成员 属性
再绑定方法上添加 :@property 调用的时候就不用加 “方法()”了
3.4 成对方法
在方法中:
3.5 迭代器
定义:
1. 类中定义了__iter__和__next__两个方法
2. __iter__方法需要返回对象本身
3. __next__方法,返回下一个数据,如果没有数据,则需要抛出一个StopIteration异常
|