主题
Python是一种高级编程语言,以其简洁、易读、易学的语法和广泛的应用场景而闻名。
随着AI技术的平民化,很有必要掌握该门语言
配置pip
bash
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
镜像加速:
-i https://pypi.tuna.tsinghua.edu.cn/simple
配置Jupyter
bash
jupyter notebook --generate-config --allow-root
jupyter notebook password
#配置 jupyter_notebook_config.py
c.NotebookApp.ip='*'
c.NotebookApp.password = u'sha:ce...刚才复制的那个密文'
c.NotebookApp.open_browser = False
c.NotebookApp.port =9998 #可自行指定一个端口, 访问时使用该端口
启动服务
bash
jupyter notebook --allow-root
测试
数据类型
查看更多: https://t.zsxq.com/uC6Pm
常用API
拷贝字符串到剪贴板
python
import pyperclip
pyperclip.copy(imgLink)
pip install pyperclip
获取当前用户的下载目录
python
import ctypes.wintypes
def getDocPath(pathID=5):
'''path=5: My Documents'''
buf= ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
ctypes.windll.shell32.SHGetFolderPathW(None, pathID, None, 0, buf)
return buf.value
downloadPath = os.path.join(os.path.dirname(getDocPath(0)),"Downloads")
print(downloadPath)
C:\Users\wangx\Downloads
获取主机名
python
os.popen('hostname').read().replace("\n","")
DESKTOP-7O54MHN
函数
main函数
python
if __name__ =="__main__":
#相当于执行pytest test_xxx.py命令
pytest.main(["test_xxxx.py"])
if __name__=="__main__"
该语句主要判断代码是从该.py文件直接运行,还是被其他文件导入或调用后才运行
name__是Python内置变量,存放了当前的模块名称。当模块直接运行时,模块名为__main。
如果从该.py文件直接运行(例如在调试时,就直接从当前文件运行),就会执行pytest测试,执行的文件名称为test_xxx.py
。
高阶函数
高阶:参数是函数、返回值是函数
常见的高阶函数:map、reduce、filter、apply
apply 在 Python2.3 被移除,reduce 被放在 functools 包中
推导式和生成器表达式可以替代 map 和 filter 函数
map(函数, 序列) 将序列中每个值传入函数,处理完成返回为 map 对象
python
number = list(range(11))
def square(x):
return x**2
print(list(map(square, number)))
print(dir(map(square, number)))
filter(函数,序列)将序列中每个值传入函数,符合函数条件的返回为 filter 对象
时间函数
获取当前时间戳(天)
python
import time
def get_today():
now = int(time.time())
timeArray = time.localtime(now)
today = time.strftime("%Y-%m-%d", timeArray)
# print (today)
return today
2022-12-03
获取当前时间戳(秒)
python
import time
def get_ts():
now = int(time.time())
timeArray = time.localtime(now)
ts = time.strftime("%Y%m%d%H%M%S", timeArray)
print (ts)
return ts
20221203225816
python
import time
def get_ts():
now = int(time.time())
timeArray = time.localtime(now)
ts = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print (ts)
return ts
2022-12-03 22:58:16
文件处理
七牛云sdk上传指定文件夹:https://t.zsxq.com/gdgE7
下载文件
python
url = sys.argv[1]
r = requests.get(url)
localfile = os.path.basename(url)
with open(localfile, "wb") as f:
f.write(r.content)
压缩打包
python
import zipfile
import os
class ZipHelper:
def __init__(self):
self.file_count = 0
def zip_dir(self,dirname,zipfilename):
filecount = 0
filelist = []
if os.path.isfile(dirname):
filelist.append(dirname)
else :
for root, dirs, files in os.walk(dirname):
for name in files:
filelist.append(os.path.join(root, name))
zf = zipfile.ZipFile(zipfilename, "w", zipfile.zlib.DEFLATED)
for tar in filelist:
arcname = tar[len(dirname):]
print ("package >>",arcname)
zf.write(tar,arcname)
self.file_count += 1
zf.close()
# zip_helper = ZipHelper()
# zip_helper.zip_dir("./","test.zip")
# print(os.path.getsize("test.zip"))
读写xlsx
python
from openpyxl import load_workbook
class Excel_XLSX:
def __init__(self,filepath):
self.filepath = filepath
self.wb = load_workbook(self.filepath)
self.ws = self.wb['Sheet1']
def read(self):
for row in self.ws.rows:
for cell in row:
print(row, cell.value)
def change_data(self,columns,values):
rows = self.ws.max_row+1
for index in range(len(columns)):
key = str(columns[index]) + str(rows)
self.ws[key] = str(values[index])
# 是否有id
def is_exists(self,item_id):
for row in self.ws.rows:
row_values = []
for cell in row:
row_values.append(cell.value)
if item_id in row_values:
return True
return False
def save(self):
self.wb.save(self.filepath)
if __name__ == '__main__':
excel_xlsx = Excel_XLSX('upload_list.xlsx')
excel_xlsx.change_data(['A','B','C','D'],["100","作品名称",'照片名称','拍摄时间','照片路径'])
print(excel_xlsx.is_exists("xxxxx产品1-1"))
# excel_xlsx.save()
读xls
python
import xlrd
xls_path = "xxxx.xls"
data = xlrd.open_workbook(xls_path)
table = data.sheet_by_index(0)
nrows = table.nrows
for rowx in range(1,nrows):
imageName = table.row(rowx)[0].value
读写pdf
python
import pdfminer
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfpage import PDFPage, PDFTextExtractionNotAllowed
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfdevice import PDFDevice
from pdfminer.layout import LAParams
from pdfminer.converter import PDFPageAggregator
fp = open('xxxx.pdf', 'rb')
# Create a PDF parser object associated with the file object
parser = PDFParser(fp)
# Create a PDF document object that stores the document structure.
# Password for initialization as 2nd parameter
document = PDFDocument(parser)
# Check if the document allows text extraction. If not, abort.
if not document.is_extractable:
raise PDFTextExtractionNotAllowed
# Create a PDF resource manager object that stores shared resources.
rsrcmgr = PDFResourceManager()
# Create a PDF device object.
#device = PDFDevice(rsrcmgr)
# BEGIN LAYOUT ANALYSIS.
# Set parameters for analysis.
laparams = LAParams(
char_margin=10.0,
line_margin=0.2,
boxes_flow=0.2,
all_texts=False,
)
# Create a PDF page aggregator object.
device = PDFPageAggregator(rsrcmgr, laparams=laparams)
# Create a PDF interpreter object.
interpreter = PDFPageInterpreter(rsrcmgr, device)
# loop over all pages in the document
for page in PDFPage.create_pages(document):
# read the page into a layout object
interpreter.process_page(page)
layout = device.get_result()
for obj in layout._objs:
if isinstance(obj, pdfminer.layout.LTTextBoxHorizontal):
print(obj.get_text().replace("\n",""))
多媒体处理
修改微信小程序二维码logo:https://t.zsxq.com/EChp1
生成视频序列帧:https://t.zsxq.com/EXyTF
基于kokoro实现tts:https://t.zsxq.com/7BdHk
操作数据库
操作mysql: https://t.zsxq.com/ddQAq
异步编程
bash
pip install aiohttp hypercorn asgiref
查看代码:https://gitee.com/PatrickW/flask-web/blob/master/src/case8_async/app.py