一、shelve模块

         shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写;key必须为字符串,而值可以是python所支持的数据类型

            import shelve
info1={'age':18,'height':180,'weight':80}
info2={'age':73,'height':150,'weight':80} d=shelve.open('db.shv')
d['egon']=info1
d['alex']=info2
d.close() d=shelve.open('db.shv')
# print(d['egon'])
# print(d['alex'])
d.close() d=shelve.open('db.shv',writeback=True)
d['alex']['age']=10000
# print(d['alex'])
d.close() 二 、xml模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不
在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的接口还主要是xml。
xml的格式就是通过<>节点来区别数据结构的:
xml协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml:
print(root.iter('year')) #全文搜索
print(root.find('country')) #在root的子节点找,只找一个
print(root.findall('country')) #在root的子节点找,找所有
三、re模块 什么是正则?
 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法。或者说:正则就是
用来描述一类事物的规则。(在Python中)它内嵌在Python中,并通过 re 模块实现。正则表达式模式被编译成一系列
的字节码,然后由用 C 编写的匹配引擎执行。 常用匹配模式(元字符):

        

        重复匹配:.   ?   *   +  {m,n}  .*  .*?

            1、.:代表除了换行符外的任意一个字符
print(re.findall('a.c','abc a1c aAc aaaaaca\nc'))
print(re.findall('a.c','abc a1c aAc aaaaaca\nc',re.DOTALL)) 2、?:代表左边那一个字符重复0次或1次
print(re.findall('ab?','a ab abb abbb abbbb abbbb')) 3、*:代表左边那一个字符出现0次或无穷次
print(re.findall('ab*','a ab abb abbb abbbb abbbb a1bbbbbbb')) 4、+ :代表左边那一个字符出现1次或无穷次
print(re.findall('ab+','a ab abb abbb abbbb abbbb a1bbbbbbb')) 5、{m,n}:代表左边那一个字符出现m次到n次
print(re.findall('ab?','a ab abb abbb abbbb abbbb'))
print(re.findall('ab{0,1}','a ab abb abbb abbbb abbbb')) print(re.findall('ab*','a ab abb abbb abbbb abbbb a1bbbbbbb'))
print(re.findall('ab{0,}','a ab abb abbb abbbb abbbb a1bbbbbbb')) 6、.*:匹配任意长度,任意的字符=====》贪婪匹配
print(re.findall('a.*c','ac a123c aaaac a *123)()c asdfasfdsadf')) 7、.*?:非贪婪匹配(推荐使用)
print(re.findall('a.*?c','a123c456c')) 8、():分组
print(re.findall('(alex)_sb','alex_sb asdfsafdafdaalex_sb')) print(re.findall(
'href="(.*?)"',
'<li><a id="blog_nav_sitehome" class="menu" href="http://www.cnblogs.com/">博客园</a></li>')
)
<li><a id="blog_nav_sitehome" class="menu" href="http://www.cnblogs.com/">博客园</a></li> 9、[]:匹配一个指定范围内的字符(这一个字符来自于括号内定义的)
print(re.findall('a[0-9][0-9]c','a1c a+c a2c a9c a11c a-c acc aAc')) 当-需要被当中普通符号匹配时,只能放到[]的最左边或最 右边
print(re.findall('a[-+*]c','a1c a+c a2c a9c a*c a11c a-c acc aAc'))
print(re.findall('a[a-zA-Z]c','a1c a+c a2c a9c a*c a11c a-c acc aAc')) []内的^代表取反的意思
print(re.findall('a[^a-zA-Z]c','a c a1c a+c a2c a9c a*c a11c a-c acc aAc'))
print(re.findall('a[^0-9]c','a c a1c a+c a2c a9c a*c a11c a-c acc aAc')) print(re.findall('([a-z]+)_sb','egon alex_sb123123wxxxxxxxxxxxxx_sb,lxx_sb')) 10、| :或者
print(re.findall('compan(ies|y)','Too many companies have gone bankrupt, and the next one is my company')) /11、(?:):代表取匹配成功的所有内容,而不仅仅只是括号内的内容
print(re.findall('compan(?:ies|y)','Too many companies have gone bankrupt, and the next one is my company')) print(re.findall('alex|sb','alex sb sadfsadfasdfegon alex sb egon')) re模块的其他方法: print(re.findall('e','alex make love') ) #['e', 'e', 'e'],返回所有满足匹配条件的结果,放在列表里 print(re.search('e','alex make love').group()) #e,只到找到第一个匹配然后返回一个包含匹配信息的对象,该对象
可以通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None。 print(re.match('e','alex make love')) #None,同search,不过在字符串开始处进行匹配,完全可以用search+^代替match print(re.split('[ab]','abcd')) #['', '', 'cd'],先按'a'分割得到''和'bcd',再对''和'bcd'分别按'b'分割
info=r'get :a.txt\3333/rwx'
print(re.split('[ :\\\/]',info)) print('===>',re.sub('a','A','alex make love')) #===> Alex mAke love,不指定n,默认替换所有
print(re.sub('(.*?)(egon)(.*?)(egon)(.*?)',r'\1\2\3EGON\5','123 egon is beutifull egon 123'))
(123 )(egon)( is beutifull )(egon)( 123) print(re.sub('(lqz)(.*?)(SB)',r'\3\2\1',r'lqz is SB'))
print(re.sub('([a-zA-Z]+)([^a-zA-Z]+)([a-zA-Z]+)([^a-zA-Z]+)([a-zA-Z]+)',r'\5\2\3\4\1',r'lqzzzz123+ is SB'))
(lqzzzz)(123+ )(is)( )(SB) pattern=re.compile('alex') #将re.compile('alex')赋值个pattern 可以再次直接调用pattern使用
print(pattern.findall('alex is alex alex'))
print(pattern.findall('alexasdfsadfsadfasdfasdfasfd is alex alex'))

最新文章

  1. VisualStudio2013 如何打开之前版本开发的(.vdproj )安装项目
  2. overload、overwrite、override
  3. 【代码笔记】iOS-图文混排(HBLabelDemo)
  4. ORA-01113: file xxxx needs media recovery
  5. Android Intent
  6. nginx php rewrite配置
  7. KEIL MDK STM32如何建立工程
  8. 【UVA 1583】Digit Generator
  9. linux gitlab nginx 安装 配置
  10. js 字符串转化成数字:(实例:用正则检测大于0的正数,最多保留4位小数)
  11. 《OD学算法》常用算法集合
  12. 总结c++ primer中的notes
  13. C++实现离散余弦变换(参数为Eigen矩阵)
  14. hql查询技巧
  15. Delphi 实现Ini文件参数与TEdit和TCheckBox绑定(TSimpleParam)
  16. 干净的架构The Clean Architecture
  17. Dynamics CRM 多个Form显示不同的Ribbon按钮
  18. linux目录结构图
  19. Django web框架篇:基础
  20. 数据交换格式与SpringIOC底层实现

热门文章

  1. javaX邮件发送
  2. Java项目启动时候报Neither the JAVA_HOME nor the JRE_HOME environment variable is defined 解决办法
  3. NO2——最短路径
  4. [boost-3] 函数对象
  5. C#-WinForm控制输入框只接受数字输入
  6. 【转】cocos2dx 3.x 集成protobuf
  7. feof问题
  8. Jekyll 使用入门
  9. Reabble.com-KindleRSS新闻杂志订阅
  10. cf 442 D. Olya and Energy Drinks