# pytools **Repository Path**: dashantu/pytools ## Basic Information - **Project Name**: pytools - **Description**: python 开发的小工具 - **Primary Language**: Python - **License**: GPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-09-23 - **Last Updated**: 2022-01-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: Python ## README http://192.168.1.129:8000?token= ``` IPython notebook(Jupyter notebook)指定IP和端口运行 jupyter notebook --no-browser --port 6000 --ip=192.168.1.103 ``` # pytools ### 介绍 python 开发的小工具 ![](cytoimg/source/mask.jpg) ## 文字生成图片功能 cytoimg ![](cytoimg/source/output/1632415686.jpg) ## 图片生成素描 cv2sumiao cv2pictoline ![](./cv2sumiao/sm.jpg) ## 基础小知识 ## 数据类型 - 变量 - int,float - bool:true,false - 类型 type() ## 基本运算符 - 加减乘除(+ - * / ) - 除法结果是 float 类型 ## 算术运算 - 取模运算:% - 幂运算:** - 整除运算: // ## 赋值运算符 -等于 =,加 +=,减 -=,乘 *=,除 /=,余 %=,幂 **=,整除 //= ## 进制运算(十,二,八) - 十转二:bin(n) - 十转八:oct(n) - 十转十六:hex(n) - 二转十:int(str,2) - 八转十:init(str,8) ## 按位运算(二进制) - 与运算:$ - 哉运算:| - 异或运算:^ - 取反运算:~ - 左右移运算:<<,>> ## 条件控制 - if (): elif(): else: ## 输入 - input("write:") ## 项目练习 - 计算器功能 - 类型转换 int(str),str(number) # 字符串 - ',",""" ## 字符操作 -可循环:s[0] ## 切片操作 - 选择操作:s[0:5] == s >=0 && s<6 - 连接操作:+ - 更新操作:s[0:5]+s2 ## 成员运算 - 包含: in 返回 bool类型 - 不包含: not in ## 转义运算 - \ - 换行 \n,制表符 \t,换行 \r (只输出\r后面的) - 强制字符输出:r(),R() ## 字符格式化输出 - print(" %s %d "%("str",10)) ## 内建函数 - str.find(s) 返回位置 - str.lower() .upper() 大小写 - str.__len__() 返回长度 - str.isspace() 是否是空格 - str.replace(s,s1) 替换 ## 列表(数组) - list:[] - list[n],list[2:] - list1[n]=str - list1.append(str) - list1+=[str,n] - del list1[1] - 返回长度:len(list1) - 移除:list1.pop(n) 返回这个移除数据 list1.remove(n) - 排序:list1.sort() - 查找:list1.index(str) ## 嵌套列表 - list:[[],[]...] ## 元组(tuple) 不能修改 - (,) ## 集合(set)无序不重复 - set(str),{item,} ### 集合运算 - & - | - ^ - set1.add(str) - set1.remove(str) - set1.pop() 随机删除 - len(set1) - set1.clear() - str in set1 ## 字典(对象) - d={key:value} - d.keys(),d.values() - d[name]=value - del d[name] - d.clear() - str in d ## 条件控制 - 码表查询:ord(str) chr(number) ## 成员运算 - in,not in - and or not ## 真假判断 - true,false - not true ## 身份运算 - is 是判断地址是否一致 与 == 不一样 - is not - 内存地址:id(str) ## 循环 - while condetion: - for i in list1: - for i in range(number): - break, continue - 冒泡排序: ------------------------------------ # 工程结构 - 包(__ini__.py)->模块->类->(方法,变量) - __ini__.py 引入后自动运行 # 命名规范 - 变量:小写_小写 或 驼峰法 - 常量:大写_大写 # 导入 - import file as xx - from file import file2 as xxx ------------------------------------ # 函数 - def name(p1,p2=1): return ;//参数有默认值时要放到最后面 - name(33,22) == name(p1=33,p2=22) 关键字参数 - 递归:一定要有出栈 ## 特殊函数 - 只作为当前模块使用: if __name__ == "__main__": ## 变量的作用域 - PY中没有块级作用域 - 函数中的变量只在函数内使用 ## 正则表达式 - import re - re.findall(元字符,str) 查找所有 - res.search(规则,str).group(index) 查找一个 - res.match(规则,str,re.I).group() 忽略大小写 - 元字符 - . , \w - 反义代码 - \W - 限定符号 - *,+,?,{n,},{n,m} - 多匹配 - * + - 少匹配 - ? # 练习 - 手机号匹配,邮箱合法性, --------------------------------------------- # xpath 语言 - / 根节点 - // 当前节点 - . 当前节点 - .. 次级节点 - @ 属性 - 如 - /html - body/div - //div 所有DIV - @lang - *,@* 统配符号 //* - | 和 //body/div | //body/li 选择body中的div与li元素 # beautifulsoap 库 - pip install beautifulsoup4 安装 > from bs4 import BeautifulSoup htmlfile=open(url,"r",encoding="gbk").read() str=BeautifulSoup(htmlfile,"html.parser) - 获取标签 - str.head - str.div.attrs - str.find_all("p") - str.find_all(id="box") - str.find_all("div",class="sty1") - str.find_all("a",class="sty2") - tmpstr = str.find_all("div") - re.findall(">(.{2,5})",str(tmpstr)) 正则使用 # scrapy 框架 - pip install scrapy 安装 > import scraphy html=scrapy.Request(url)