2019年6月12日 re6 爱的代价

import re,logging
ret1=re.findall('(abc)+','abcabcabc')#有分组的,优先先拿出来
print(ret1)

ret2=re.findall('(?:abc)+','abcabcabc')#使用?: 降低分组的优先级,显示所有匹配内容
print(ret2)

ret3=re.findall('abc+','abcabcabcgda')#这里是c 有 1到N次
print(ret3)

logging.debug('debug')
logging.info('info')
logging.warning('warning')
logging.error('error')
logging.critical('critical')

>>>

WARNING:root:warning
ERROR:root:error
CRITICAL:root:critical
['abc']
['abcabcabc']
['abc', 'abc', 'abc']

配置文件config 创建

#配置文件模块
import configparser
config=configparser.ConfigParser() #VIP,有了该对象,相当于有了个空字典

config['DEFAULT']={
    'Server':'45',
    'Compression':'Yes',
    'Level':'g'
}

config['bit']={}
config['bit']['user']='sxj'

config['topsecret']={}
H=config['topsecret']
H['host']='500'
H['forward']='No'

with open ('example.ini','w') as f:
    config.write(f)#把句柄作为内容,写入文件中,比较特殊的方法

》》》example.ini  》》》

[DEFAULT]
server = 45
compression = Yes
level = g

[bit]
user = sxj

[topsecret]
host = 500
forward = No
import configparser
config=configparser.ConfigParser() #VIP,有了该对象,相当于有了个空字典

#######查###########
config.read('example.ini')
print(config.sections()) #打印除了default 的块名

print('byte' in config) #VIP  判断byte 这个块是否在config 包含的块中

print(config['bit']['user']) #直接读取 user 内容

for key in config['bit']:
    print('xxxxx'+key) #注意Default,如果遍历其他key,default 还是会遍历出来

######删,改,查############
config.add_section('yuan')
config.set('yuan','k1','11111')#添加1组 新键值对
config.set('yuan','k2','22222')
#config.remove_section('topsecret') #删除块操作
config.remove_option('yuan','k2')# 删除K2键值对


config.write(open('i.ctg','w'))#VIP:重新输入到配置文件i.ctg,形成新文件 ,这种写法不用写closed 推荐

》》》

['bit', 'topsecret']
False
sxj
xxxxxuser
xxxxxserver
xxxxxcompression
xxxxxlevel

原文地址:https://www.cnblogs.com/python1988/p/11012074.html