Configparser模块、re模块、元字符

标题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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值