首页 > 其他 > 详细

day27 模块:正则re, configparser, subprocess

时间:2017-06-27 18:14:14      阅读:274      评论:0      收藏:0      [点我收藏+]

Python之路,Day15 = Python基础15

 

re 模块补充

 1 ret = re.findall("c.d", "abc\nd", re.S)     
 2     # 后面参数用来修改模式,这个模式下 . 可以匹配所有的字符,包括换行符
 3 
 4 
 5 
 6 re.split()
 7     re.split("\d+", "hello23world12dfae3dge")
 8     >>>["hello", "world", "dfae", "dge"]
 9     可以第三个参数,最大分割次数。
10 
11     re.split("(\d+)", "hello23world12dfae3dge")
12     >>>["hello", "23", "world", "12", "dfae", "3", "dge"]
13     分隔符用括号括起来,可以保留分隔符
14     
15     
16 re.sub()
17     re.sub(old, new, str, count)
18     re.sub(规则, 新的内容, 处理的字符串)
19     有返回值?
20     
21 re.subn()
22     类似于 sub, 返回结果为一个元祖,第一个为返回的结果,第二个元素为替换的个数
23 
24 
25 re.compile()      # 编译
26     
27 re.finditer()
28     结果为可迭代对象
29     res = re.finditer("","")
30     next(res).group()
31     # 封装到迭代器里面的是一个个对象

 

configparser 模块

  # 模块:用于文件处理
  # 可处理的文件类似于配置文件,文件的内容类似于嵌套的字典,文件格式:

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
  
[bitbucket.org]
User = hg
  
[topsecret.server.com]
Port = 50022
ForwardX11 = no
注:[DEFAULT] 为默认,里面存放的内容为下面都有的内容

用python生成这种文件

 1 import configparser
 2 
 3 config = configparser.ConfigParser()
 4 
 5 config["DEFAULT"] = {‘ServerAliveInterval‘: ‘45‘,
 6                       ‘Compression‘: ‘yes‘,
 7                      ‘CompressionLevel‘: ‘9‘,
 8                      ‘ForwardX11‘:‘yes‘
 9                      }
10 
11 config[‘bitbucket.org‘] = {‘User‘:‘hg‘}
12 
13 config[‘topsecret.server.com‘] = {‘Host Port‘:‘50022‘,‘ForwardX11‘:‘no‘}
14 
15 with open(‘example.ini‘, ‘w‘) as configfile:
16 
17    config.write(configfile)

 

 查找文件

 1 import configparser
 2 
 3 config = configparser.ConfigParser()
 4 
 5 #---------------------------查找文件内容,基于字典的形式
 6 
 7 print(config.sections())        #  []
 8 
 9 config.read(‘example.ini‘)
10 
11 print(config.sections())        #   [‘bitbucket.org‘, ‘topsecret.server.com‘]
12 
13 print(‘bytebong.com‘ in config) # False
14 print(‘bitbucket.org‘ in config) # True
15 
16 
17 print(config[‘bitbucket.org‘]["user"])  # hg
18 
19 print(config[‘DEFAULT‘][‘Compression‘]) #yes
20 
21 print(config[‘topsecret.server.com‘][‘ForwardX11‘])  #no
22 
23 
24 print(config[‘bitbucket.org‘])          #<Section: bitbucket.org>
25 
26 for key in config[‘bitbucket.org‘]:     # 注意,有default会默认default的键
27     print(key)
28 
29 print(config.options(‘bitbucket.org‘))  # 同for循环,找到‘bitbucket.org‘下所有键
30 
31 print(config.items(‘bitbucket.org‘))    #找到‘bitbucket.org‘下所有键值对
32 
33 print(config.get(‘bitbucket.org‘,‘compression‘)) # yes       get方法取深层嵌套的值

 

增删改操作

 

import configparser

config = configparser.ConfigParser()

config.read(‘example.ini‘)

config.add_section(‘yuan‘)



config.remove_section(‘bitbucket.org‘)
config.remove_option(‘topsecret.server.com‘,"forwardx11")


config.set(‘topsecret.server.com‘,‘k1‘,‘11111‘)
config.set(‘yuan‘,‘k2‘,‘22222‘)

config.write(open(‘new2.ini‘, "w")) 

 

day27 模块:正则re, configparser, subprocess

原文:http://www.cnblogs.com/alwaysInMe/p/7086262.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!