Python学习笔记----基础篇10----模块2

8)json& pickle

用于序列化的两个模块

json,用于处理字符串和python数据类型间进行转换

pickle,用于python特有的类型和python的数据类型间进行站换

Json模块提供了四个功能:dumps、dump、loads、load

pickle模块提供了四个功能:dumps、dump、loads、load

>>> import pickle
>>> data = {'k1':1234,'k2':'hello ykyk'}
>>> p_str = pickle.dumps(data)
>>> print(p_str)
b'\x80\x03}q\x00(X\x02\x00\x00\x00k1q\x01M\xd2\x04X\x02\x00\x00\x00k2q\x02X\n\x00\x00\x00hello ykykq\x03u.'

import pickle

data = {'k1':'12345','k2':'hello'}

p_str = pickle.dumps(data)

with open('/test/practise/tina.pk','w') as fp:
     pickle.dump(data,fp)



import json

data = {'k1':'12345','k2':'hello'}

p_str = json.dumps(data)

with open('/test/practise/tina','w') as fp:
         json.dump(data,fp)


9)shelve模块

import shelve

d = shelve.open('shelve_test') #打开一个文件

class Test(object):

def __init__(self,n):

self.n = n

t = Test(123)

t2 = Test(123334)

name = ["alex","rain","test"]

d["test"] = name #持久化列表

d["t1"] = t      #持久化类

d["t2"] = t2

d.close()


10)xml处理模块

xml是实现不同语言或者程序之间进行数据交换的协议。

样例文件

<?xml version="1.0"?>

<data>

<country name="Liechtenstein">

<rank updated="yes">2</rank>

<year>2008</year>

<gdppc>141100</gdppc>

<neighbor name="Austria" direction="E"/>

<neighbor name="Switzerland" direction="W"/>

</country>

<country name="Singapore">

<rank updated="yes">5</rank>

<year>2011</year>

<gdppc>59900</gdppc>

<neighbor name="Malaysia" direction="N"/>

</country>

<country name="Panama">

<rank updated="yes">69</rank>

<year>2011</year>

<gdppc>13600</gdppc>

<neighbor name="Costa Rica" direction="W"/>

<neighbor name="Colombia" direction="E"/>

</country>

</data>



使用方法:

import xml.etree.ElementTree as ET

tree = ET.parse("xmltest.xml")

root = tree.getroot()

print(root.tag)

#遍历xml文档

for child in root:

print(child.tag, child.attrib)

for i in child:

print(i.tag,i.text)

#只遍历year 节点

for node in root.iter('year'):

print(node.tag,node.text)


修改& 删除

import xml.etree.ElementTree as ET

tree = ET.parse("xmltest.xml")

root = tree.getroot()

#修改

for node in root.iter('year'):

new_year = int(node.text) + 1

node.text = str(new_year)

node.set("updated","yes")

tree.write("xmltest.xml")

#删除node

for country in root.findall('country'):

rank = int(country.find('rank').text)

if rank > 50:

root.remove(country)

tree.write('output.xml')


11) re正则表达式模块

'.' 默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行

'^' 匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r"^a","\nabc\neee",flags=re.MULTILINE)

'$' 匹配字符结尾,或e.search("foo$","bfoo\nsdfsf",flags=re.MULTILINE).group()也可以

'*' 匹配*号前的字符0次或多次,re.findall("ab*","cabb3abcbbac")  结果为['abb', 'ab', 'a']

'+' 匹配前一个字符1次或多次,re.findall("ab+","ab+cd+abb+bba") 结果['ab', 'abb']

'?' 匹配前一个字符1次或0

'{m}' 匹配前一个字符m次

'{n,m}' 匹配前一个字符n到m次,re.findall("ab{1,3}","abb abc abbcbbb") 结果'abb', 'ab', 'abb']

'|' 匹配|左或|右的字符,re.search("abc|ABC","ABCBabcCD").group() 结果'ABC'

'(...)' 分组匹配,re.search("(abc){2}a(123|456)c", "abcabca456c").group() 结果 abcabca456c

'\A' 只从字符开头匹配,re.search("\Aabc","alexabc") 是匹配不到的

'\Z' 匹配字符结尾,同$

'\d' 匹配数字0-9

'\D' 匹配非数字

'\w' 匹配[A-Za-z0-9]

'\W' 匹配非[A-Za-z0-9]

's' 匹配空白字符、\t、\n、\r , re.search("\s+","ab\tc1\n3").group() 结果 '\t'

'(?P<name>...)' 分组匹配

>>> re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})","210624199305100044").groupdict("city")
{'province': '2106', 'city': '24', 'birthday': '1993'}

常用命令

re.match 从头开始匹配

re.search 匹配包含

re.findall 把所有匹配到的字符放到以列表中的元素返回

re.splitall 以匹配到的字符当做列表分隔符

re.sub      匹配字符并替换

正则表达式定义:

正则表达式是一些用来匹配和处理文本的字符串

正则表达式语言并不是一种完备的程序设计语言,他甚至算不上是一种能够直接安装并运行的程序,更准确的说,正则表达式语言是内置于其他语言或软件产品里的“迷你”语言

posted on 2018-03-15 12:15  ykyk_dba  阅读(180)  评论(1编辑  收藏  举报

导航