日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2023/12/1 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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????? 匹配字符并替換

正則表達式定義:

正則表達式是一些用來匹配和處理文本的字符串

正則表達式語言并不是一種完備的程序設計語言,他甚至算不上是一種能夠直接安裝并運行的程序,更準確的說,正則表達式語言是內置于其他語言或軟件產品里的“迷你”語言

轉載于:https://www.cnblogs.com/ykyk1229/p/8572858.html

總結

以上是生活随笔為你收集整理的Python学习笔记----基础篇10----模块2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。