Python学习笔记----基础篇10----模块2
8)json& pickle
用于序列化的兩個模塊
json,用于處理字符串和python數(shù)據(jù)類型間進(jìn)行轉(zhuǎn)換
pickle,用于python特有的類型和python的數(shù)據(jù)類型間進(jìn)行站換
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是實(shí)現(xiàn)不同語言或者程序之間進(jìn)行數(shù)據(jù)交換的協(xié)議。
樣例文件
<?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 節(jié)點(diǎn)
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正則表達(dá)式模塊
'.' 默認(rèn)匹配除\n之外的任意一個字符,若指定flag DOTALL,則匹配任意字符,包括換行
'^' 匹配字符開頭,若指定flags MULTILINE,這種也可以匹配上(r"^a","\nabc\neee",flags=re.MULTILINE)
'$' 匹配字符結(jié)尾,或e.search("foo$","bfoo\nsdfsf",flags=re.MULTILINE).group()也可以
'*' 匹配*號前的字符0次或多次,re.findall("ab*","cabb3abcbbac")? 結(jié)果為['abb', 'ab', 'a']
'+' 匹配前一個字符1次或多次,re.findall("ab+","ab+cd+abb+bba") 結(jié)果['ab', 'abb']
'?' 匹配前一個字符1次或0次
'{m}' 匹配前一個字符m次
'{n,m}' 匹配前一個字符n到m次,re.findall("ab{1,3}","abb abc abbcbbb") 結(jié)果'abb', 'ab', 'abb']
'|' 匹配|左或|右的字符,re.search("abc|ABC","ABCBabcCD").group() 結(jié)果'ABC'
'(...)' 分組匹配,re.search("(abc){2}a(123|456)c", "abcabca456c").group() 結(jié)果 abcabca456c
'\A' 只從字符開頭匹配,re.search("\Aabc","alexabc") 是匹配不到的
'\Z' 匹配字符結(jié)尾,同$
'\d' 匹配數(shù)字0-9
'\D' 匹配非數(shù)字
'\w' 匹配[A-Za-z0-9]
'\W' 匹配非[A-Za-z0-9]
's' 匹配空白字符、\t、\n、\r , re.search("\s+","ab\tc1\n3").group() 結(jié)果 '\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 以匹配到的字符當(dāng)做列表分隔符
re.sub????? 匹配字符并替換
正則表達(dá)式定義:
正則表達(dá)式是一些用來匹配和處理文本的字符串
正則表達(dá)式語言并不是一種完備的程序設(shè)計語言,他甚至算不上是一種能夠直接安裝并運(yùn)行的程序,更準(zhǔn)確的說,正則表達(dá)式語言是內(nèi)置于其他語言或軟件產(chǎn)品里的“迷你”語言
轉(zhuǎn)載于:https://www.cnblogs.com/ykyk1229/p/8572858.html
總結(jié)
以上是生活随笔為你收集整理的Python学习笔记----基础篇10----模块2的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对request.getSession(
- 下一篇: 3.4 内置函数(1)