正则表达式 re 模块
正则是一种对字符和特殊字符操作的逻辑公式(日志处理或文件处理)
从特定的字符中用正则表达式来过滤:主要用于模式匹配和替换工作
\d:数字0~9
re.match('以此字符开头',匹配的对象) #带了+重复匹配
re.search
import restr='ab123c9999785defghijk\tl\nmn@@#@opqrstuvwxyzabccccaabc'print(re.match('ab',str)) #匹配字符串是否以该字符开头print(re.search('b',str)) #匹配字符串中包含值,第一次找到返回位置(索引)print(re.findall('a',str)) #返回列表['a', 'a', 'a', 'a']或[]p=re.compile('\d')print(p.findall(str)) #编译规则print(re.findall('^a....c',str)) #.表示元字符,代表一个任意字符(\n除外)print(re.findall('..c$',str)) # $匹配以c结尾的print(re.findall('abc*',str)) #*匹配*前一个字符0次或多次print(re.findall('abc+',str)) #+匹配+前一个字符一次或多次print(re.findall('abc?',str)) #?匹配?前一个字符一次或0次print(re.findall('c{3}a',str)) #()匹配(n)前一个字符n次print(re.findall('c{1,2}a',str)) #()匹配(n,m)前一个字符n~m次print(re.findall('\d{3}',str)) #\d匹配0~9的数字print(re.findall('\D{10}',str)) #\D匹配非数字 {n}n个一组,前后不足的不要print(re.findall('\s',str)) #\s匹配空白字符print(re.findall('\S',str)) #\S匹配非空白字符print(re.findall('\w',str)) #\w匹配数字,字母print(re.findall('\W',str)) #\s匹配非数字、字母字符
xlrd模块 excel读
支持xlsx xls (不支持csv文件,csv文件可用csv的模块)
import xlrdurl=r'd:\zz\zz.xls'data=xlrd.open_workbook(url)print(data.sheet_names()) #获取sheet名称print(data.sheet_by_name('小周')) #通过sheet名称查找文件print(data.sheet_by_index(0)) #通过sheet索引查找文件print(data.nsheets) #获取sheet的数量print(data.sheets()[0].name) #获取第一个sheet的名称print(data.sheet_by_name('小周').nrows) #获取sheet文件总行数print(data.sheets()[0].row_values(0)) #获取sheet文件第一行内容print(data.sheet_by_index(0).row(0)) #获取sheet文件第一行的类型print(data.sheet_by_index(0).row_types(0)) #获取sheet文件第一行的类型print(data.sheet_by_index(0).row_values(0,2,4)) #获取sheet文件第一行的,索引2到4(不包含)列的值print(data.sheet_by_index(0).col_values(1,2,4)) #获取sheet文件第二行的,索引2到4(不包含)列的值
xlwt模块 excel写
json
sys
类的定义:不要纯数字,不用保留字,不用特殊符号,见名知义,驼峰命名
class 类名称()
class Student(object): #定义类
head=1 #类变量
def __init__(self,name,age): #构造方法
self.name=name #把参数变成实例变量
self.age=age
def xinxi(self): #实例方法
return f'{self.name}{self.age}'
对象的声明:
对象名=类名()
|
|