re模块
1 import re 2 string = 'bottle bag big apple' # 字符串文本 3 pattern = 'b' # 正则表达式字符串 4 regex = re.compile(pattern, flags=0) # compile为编译,flags可选编译模式缺省默认模式 5 # 单次匹配 6 print(re.match(pattern, string, flags=0)) # re.match从字符串的开头开始匹配,返回match对象 7 print(regex.match(string, pos=0)) # regex.match中pos可指定从第几个字符开始匹配 8 # <_sre.SRE_Match object; span=(0, 1), match='b'> 9 # 全文搜索 10 print(re.findall(pattern, string, flags=0)) # re.findall从字符串的开头开始全文匹配,返回匹配的列表 11 print(regex.findall(string, pos=0)) # regex.findall中pos可指定从第几个字符开始匹配 12 print(re.finditer(pattern, string, flags=0)) # re.finditer从字符串的开头开始全文匹配,返回迭代器,迭代内容是match对象 13 print(regex.finditer(string, pos=0)) # regex.finditer中pos可指定从第几个字符开始匹配 14 # ['b', 'b', 'b', 'b'] 15 # <callable_iterator object at 0x0000023ADB782C88> 16 # 匹配替换 17 repl = 'a' 18 print(re.sub(pattern, repl, string, count=0, flags=0)) # re.sub返回一个替换后的文本,count控制替换次数,缺省不限制次数 19 print(regex.sub(repl, string, count=0)) # regex.sub返回一个替换后的文本 20 print(re.subn(pattern, repl, string, count=0, flags=0)) # re.subn返回一个元组,包括替换后文本和替换次数 21 print(regex.subn(repl, string, count=0)) # regex.subn返回一个元组,包括替换后文本和替换次数 22 # 切割 23 print(re.split(pattern, string, maxsplit=0, flags=0)) # re.split返回切割后的列表,maxsplit控制切割次数,缺省不限制次数 24 # ['', 'ottle ', 'ag ', 'ig apple'] 25 # 分组 26 # 在pattern中使用"()"小括号进行分组 27 pattern = '(?P<name1>bw+)s(?P<name2>bw+)' # ?P<>设置分组名字 28 result = re.match(pattern, string, flags=0) 29 print(result.group(), result.group(1, 2), result.groups()) # guoup返回分组匹配的文本,缺省是0返回所有,也可以用guoups返回一个元组 30 # bottle 31 # bag ('bottle', 'bag') ('bottle', 'bag') 32 print(result.groupdict()) # groupdict返回分组名称与匹配文本组成的字典 33 # {'name1': 'bottle', 'name2': 'bag'} 34 print(re.findall(pattern, string, flags=0)) # [('bottle', 'bag')],分组后findall返回的列表元素是元组
csv模块
csv文件
csv是一个被行分隔符,列分隔符划分成行和列的文件,行分隔符用 ,列分隔符用逗号或制表符
csv模块的写和读
1 import csv 2 rows = [[1, 2, 3], [1, 'a', 'b'], [2, ['c', 'd'], {'e': 'f', 'g': 'h'}]] 3 with open('test.csv', 'w+', newline='') as f: 4 writer = csv.writer(f) 5 writer.writerow(rows[0]) # 写入1行 6 writer.writerows(rows[1:]) # 写入多行 7 with open('test.csv', newline='') as f: 8 reader = csv.reader(f) # 生成迭代器 9 for i in reader: # 续行打印 10 print(i)
ini处理
mysql.ini
1 [DEFAULT] 2 a = test 3 4 [mysql] 5 default-character-set=utf8 6 7 [mysqld] 8 datadir =/dbserver/data 9 port = 33060 10 character-set-server=utf8 11 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
中括号部分称为section,译作节,区,段
每一个section内都是key=value的键值对,key称为option选项
DEFAULT是缺省section的名字,必须大写
configparser模块
1 from configparser import ConfigParser 2 filename = 'mysql.ini' 3 newfilename = 'test.ini' 4 cfg = ConfigParser() 5 print(cfg.read(filename)) # ['mysql.ini'], 打印文件名称 6 print(cfg.sections()) # ['mysql', 'mysqld'],打印section列表 7 print(cfg.has_section('mysqld')) # has_section检查是否存在该section,返回True或False 8 for k, v in cfg.items(): 9 print(type(k), k) 10 print(type(v), v) 11 print(cfg.items(k)) 12 # <class 'str'> DEFAULT 13 # <class 'configparser.SectionProxy'> <Section: DEFAULT> 14 # [('a', 'test')] 15 # <class 'str'> mysql 16 # <class 'configparser.SectionProxy'> <Section: mysql> 17 # [('a', 'test'), ('default-character-set', 'utf8')] 18 # <class 'str'> mysqld 19 # <class 'configparser.SectionProxy'> <Section: mysqld> 20 # [('a', 'test'), ('datadir', '/dbserver/data'), ('port', '33060'), ('character-set-server', 'utf8'), ('sql_mode', 'NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES')] 21 for k, v in cfg.items('mysqld'): # 指定section 22 print(type(k), k) 23 print(type(v), v) 24 # <class 'str'> a 25 # <class 'str'> test 26 # <class 'str'> datadir 27 # <class 'str'> /dbserver/data 28 # <class 'str'> port 29 # <class 'str'> 33060 30 # <class 'str'> character-set-server 31 # <class 'str'> utf8 32 # <class 'str'> sql_mode 33 # <class 'str'> NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 34 tmp = cfg.get('mysqld', 'a') # 取值 35 print(type(tmp), tmp) # <class 'str'> test 36 if cfg.has_section('test'): # 检查是否存在 37 cfg.remove_section('test') # 删除ection 38 cfg.add_section('test') # 新建section 39 cfg.set('test', 'test1', '1') # 加入键值对 40 cfg.set('test', 'test2', '2') 41 print(cfg.getint('test', 'test1')) # 1,取值转int格式 42 cfg.remove_option('test', 'test2') # 删除option 43 # 可以当字典操作 44 cfg['test']['test3'] = '3' 45 cfg['test2'] = {'a': 'b', 'c': 'd'} 46 with open(newfilename, 'w') as f: 47 cfg.write(f) # 写入新ini文件里 48 # 其它内部方式 49 for k, v in cfg._sections.items(): 50 print(k, v) 51 # mysql OrderedDict([('default-character-set', 'utf8')]) 52 # mysqld OrderedDict([('datadir', '/dbserver/data'), ('port', '33060'), ('character-set-server', 'utf8'), ('sql_mode', 'NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES')]) 53 # test OrderedDict([('test1', '1'), ('test3', '3')]) 54 # test2 OrderedDict([('a', 'b'), ('c', 'd')]) 55 for k, v in cfg._sections['mysqld'].items(): 56 print(k, v) 57 # datadir /dbserver/data 58 # port 33060 59 # character-set-server utf8 60 # sql_mode NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES