标题Configparser模块:
配置文件模块,用于生成和修改常见配置文档
在这里插入代码片import configparser
config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9'}
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
config.write(configfile)
print(config.sections())
confing.read('example.ini')
print(config.sections())
print(config.defaults())
print(config['bitbucket.org']['User'])
for key in config['bitbuck.org']:
print(key)
config.remove_setion('topsecret.server.com')
print(config.has_section('topsecret.server.com'))
config.remove_option('bitbucket.org','user')
# config.set('bitbucket.org','user','alex')
config.write(open('example.ini',"w"))
标题re模块:
1.正则表达式是用来干嘛的?
匹配 字符串的。
在这里插入代码片s='hello world'
print(s.find('llo'))
ret=s.replace('11','xx')
print(ret)
print(s.split('w'))
string提供的方法是完全匹配
就其本质而言表达式(或RE)是一种小型的、高度专业化的编程语言,
(在python中)它在嵌在python中,并通过 re 模块实现。正则表达式模式被
编译成一系列的字节码,然后由用 c 编写的匹配引擎执行。
在这里插入代码片字符匹配(普通字符,元字符):
普通字符:大多数字符和字母都会和自身匹配
>>> re.findall('alez',yanlaesxalexwpeiqi)
['alez']
元字符: . ^ $ * + ? {} [] | () \
在这里插入代码片ret=re.findall('w\w{2}l','hello world')
print(ret)
ret=re.findall('w.l','hello world')# 只能代表任意一个字符
print(ret)
ret=re.findall('w..l','hell w\t ld')
print(ret)
元字符:
在这里插入代码片import re
.通配符
ret=re.findall('w..l','hell w\t ld')
print(ret)
^
ret=re.findall('^h...o','hjasoflhello')
print(ret)
$
ret=re.findall('a..x','hfajklsdhgalexeauyx')
print(ret)
*:重复匹配[0,+oo]
ret=re.findall('.*','huagege')
print(ret)
? [0,1]
ret=re.findall('a?b','aaaabhghabfb')
print(ret)
ret=re.findall('a*b','aaaaaaab') {1,}等于{1,+oo}
print(ret)
结论:*等于{0,正无穷} +等价于{1, +00} ?等于{0,1}推荐前者
+:[1,+00]
ret=re.findall('ab+','kjldfah')
print(ret)
ret=re.findall('a+b','aaabhghabfb')
print(ret)