python之路--day17-shelve,xml和re模块

shelve模块

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

 1 import shelve
 2 # info1={'age':18,'height':180,'weight':80}
 3 # info2={'age':73,'height':150,'weight':80}
 4 #
 5 # d=shelve.open('db.shv')
 6 # d['egon']=info1
 7 # d['alex']=info2
 8 # d.close()
 9 
10 
11 # d=shelve.open('db.shv')
12 # # print(d['egon'])
13 # # print(d['alex'])
14 # d.close()
15 
16 #修改内容的时候,将 writeback = True才能修改
17 # d=shelve.open('db.shv',writeback=True)
18 # d['alex']['age']=10000
19 # # print(d['alex'])
20 # d.close()

xml模块

  xml是实现不同语言或程序之间的进行数据交换的协议,和json类型,但是json使用更简单

  xml是通过<>节点来区别数据结构

 1 <?xml version="1.0"?>
 2 <data>
 3     <country name="Liechtenstein">
 4         <rank updated="yes">2</rank>
 5         <year>2008</year>
 6         <gdppc>141100</gdppc>
 7         <neighbor name="Austria" direction="E"/>
 8         <neighbor name="Switzerland" direction="W"/>
 9     </country>
10     <country name="Singapore">
11         <rank updated="yes">5</rank>
12         <year>2011</year>
13         <gdppc>59900</gdppc>
14         <neighbor name="Malaysia" direction="N"/>
15     </country>
16     <country name="Panama">
17         <rank updated="yes">69</rank>
18         <year>2011</year>
19         <gdppc>13600</gdppc>
20         <neighbor name="Costa Rica" direction="W"/>
21         <neighbor name="Colombia" direction="E"/>
22     </country>
23 </data>
xml格式

 xml协议在各个语言里都是支持的,在python中可以用以下模块操作xml:

import xml.etree.ElementTree as ET

tree = ET.parse('a.xml')
root = tree.getroot()

三种查找方式:

 # res=root.iter('rank') # 会在整个树中进行查找,而且是查找到所有
 # for item in res:
 # # print(item)
 # print('='*50)
 # print(item.tag) # 标签名
 # print(item.attrib) #属性
 # print(item.text) #文本内容


# print(root.find('country')) #在root的子节点找,只找一个
# print(root.findall('country')) #在root的子节点找,找所有

修改:使用set方法
删除:使用remove方法

re模块

  正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法

  常用匹配模式(元字符)

w 匹配字母数字和下划线
print(re.findall('w','abc123_=+)(*&^%'))
['a', 'b', 'c', '1', '2', '3', '_']
W 匹配非字母数字下划线
print(re.findall('W','abc123_=+)(*&^%'))
['=', '+', ')', '(', '*', '&', '^', '%']
s 匹配任意空白字符,等价于【	

f】
print(re.findall('s','
abc	12
3_=+)(*&^%'))
['
', '	', '
']
S 匹配任意非空字符
print(re.findall('S','
abc	12
3_=+)(*&^%'))
['a', 'b', 'c', '1', '2', '3', '_', '=', '+', ')', '(', '*', '&', '^', '%']
d 匹配任意数字,等价于【0-9print(re.findall('d','
abc	12
3_=+)(*&^%'))
['1', '2', '3']
D 匹配任意非数字
print(re.findall('D','
abc	12
3_=+)(*&^%'))
['
', 'a', 'b', 'c', '	', '
', '_', '=', '+', ')', '(', '*', '&', '^', '%']
A 匹配字符串开始
print(re.findall('Aabc','abc123_abc=+)(*&^%abc'))
['abc']
####这个方法不建议使用 使用^
^ 匹配字符串的开头
print(re.findall('^abc','abc123_abc=+)(*&^%abc'))
['abc']
 匹配字符串结束,如果存在换行,值匹配到换行前的结束字符串
print(re.findall('abc','abc123_abc=+)(*&^%abc'))
['abc']
####这个方法不建议使用,,,使用$
$ 匹配字符串的末尾
print(re.findall('abc$','abc123_abc=+)(*&^%abc'))
['abc']
   .  匹配任意字符,除了换行符,当re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符
只能匹配一个字符
print(re.findall('a.c','abc123_a c=+)(*&^%abc')) ['abc', 'abc']
 ? 代表左边的那一个字符重复0次或1次
print(re.findall('ab?','abc123_a
c=+)(*&^%abc'))
['ab', 'a', 'ab']
 * 代表左边那一个字符出现0次或无穷次
print(re.findall('ab*','abbbbbc123_a
c=+)(*&^%abbbc'))
['abbbbb', 'a', 'abbb']
 +  代表左边那一个字符出现1次或无穷次
print(re.findall('ab+','abbbbbc123_a
c=+)(*&^%abbbc'))
['abbbbb', 'abbb']
{m,n} 代表左边那一个字符出现m次到n次
print(re.findall('ab{0,1}','abbbbbc123_abc=+)(*&^%abbbc'))
['ab', 'ab', 'ab']
 .*  匹配任意长度,任意的字符------贪婪匹配(匹配最远的)
print(re.findall('a.*c','abbbbbc123_abc=+)(*&^%abbbc'))
['abbbbbc123_abc=+)(*&^%abbbc']
 .*?  匹配任意长度,任意字符----非贪婪匹配
print(re.findall('a.*?c','abbbbbc123_abc=+)(*&^%abbbc'))
['abbbbbc', 'abc', 'abbbc']
():分组,按照规则匹配,但是只保留()里面的内容
print(re.findall('a.*?(c)','abbbbbc123_abc=+)(*&^%abbbc'))
['c', 'c', 'c']
 []   匹配一个指定范围内的字符(这个字符来自于括号内定义的)
print(re.findall('a[0-9][0-9]c','a1c a+c a2c a9c a11c a-c acc aAc'))
['a11c']


当 - 需要被当中普通符号匹配时,只能放到[]的最左边或最 右边
print(re.findall('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('compan(ies|y)','Too many companies have gone bankrupt, and the next one is my company'))
['ies', 'y']

(?:):代表取匹配成功的所有内容,而不仅仅只是括号内的内容
print(re.findall('compan(?:ies|y)','Too many companies have gone bankrupt, and the next one is my company'))
['companies', 'company']

re模块的方法

  re.findall('e','alex make love') 

      #['e', 'e', 'e'],返回所有满足匹配条件的结果,放在列表里

  re.search('e','alex make love').group()

                #e,只到找到第一个匹配然后返回一个包含匹配信息的对象,该对象可以通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None。

  re.match('e','alex make love')

        #None,同search,不过在字符串开始处进行匹配,完全可以用search+^代替match

  re.split('[ab]','abcd')

      #['', '', 'cd'],先按'a'分割得到''和'bcd',再对''和'bcd'分别按'b'分割

  re.sub('a','A','alex make love')

      #Alex mAke love,不指定n,默认替换所有

  re.sub('a','A','alex make love',1)

         #指定n,则替换n次  Alex make love

  re.sub('^(w+)(.*?s)(w+)(.*?s)(w+)(.*?)$',r'52341','alex make love')

      #love make alex   按照顺序替换

  pattern=re.compile('alex') #将正则赋值给一个变量,再次使用直接调用变量名   

      pattern.findall('alex is alex alex')
      pattern.findall('alexasdfsadfsadfasdfasdfasfd is alex alex')

      

原文地址:https://www.cnblogs.com/guodengjian/p/8759636.html