持久化-文件

文件

open函數

r:以只讀方式打開
w:寫方式打開,會覆蓋之前的內容,沒有文件的話,會自動建立
x:建立方式打開,如文件已經存在,報錯
a:append方式,以追加的方式對文件內容進行寫入
b: binary方式,二進制方式寫入
t: 文本方式打開
+: 可讀寫python

f.open(r"test01.txt",'w')
f.close()

with語句

with語句使用的技術是一種成爲上下文管理協議的技術(ContextManagementProtocal)
自動判斷文件的 做用域, 自動關閉不在使用的打開的文件句柄web

with open(r'xiaoxiongceshi01.txt','r')as f:
    #按行讀取內容
    strline=f.readline()
    #完整讀取文件知道結束
    while strline:
        print(strline)
        strline=f.readline()
# list能用打開的文件做爲參數,把文件內每一行內容做爲一個元素
with open(r'xiaoxiongceshi01.txt','r')as f:
    #以打開的文件f做爲參數,建立列表
    l=list(f)
    for line in l:
        print(line)
# read是按字符讀取文件內容
# 容許輸入參數決定讀取幾個字符,若是沒有制定,從當前位置讀取到結尾
# 不然,從當前位置讀取指定個數字符
#f.read(1)就是讀取當前一個字符

with open(r'xiaoxiongceshi01.txt','r')as f:
        print(f.read())
#使用read讀取文件,每次讀取一個,使用循環讀完
with open(r'xiaoxiongceshi01.txt','r')as f:
        strChar=f.read(1)
        while strChar:
            print(strChar)
            strChar=f.read(1)

seek(offset,from)

移動文件的讀取位置,也叫讀取指針
from的取值範圍:
0: 從文件頭開始偏移
1:從文件當前位置開始偏移
2: 從文件末尾開始偏移
移動的單位是字節(byte)
一個漢子由若干個字節構成
返回文件只針對 當前位置數據庫

#注意移動的是字節,一個漢字通常有2個字節,因此要注意不要恰好把一個漢字劈開,這樣就會報錯,亂碼
with open(r'xiaoxiongceshi01.txt','r')as f:
    f.seek(9,0)#第8個字節開始讀取
    strChar = f.read()
    print(strChar)
#打開文件,三個字符一組讀出內容,而後顯示在屏幕上,每讀一次,休息一秒鐘
import time
with open(r'xiaoxiongceshi01.txt','r')as f:
        strChar=f.read(3)
        while strChar:
            print(strChar)
            time.sleep(1)
            strChar=f.read(3)
# tell函數: 用來顯示文件讀寫指針的當前位置
with open(r'xiaoxiongceshi01.txt','r')as f:
    strChar = f.read(3)
    print(f.tell())=>4
# tell的返回數字的單位是byte
# read是以字符爲單位

文件的寫操做-write

write(str): 把字符串寫入文件
writeline(str): 把字符串按行寫入文件
區別:
write函數參數只能是字符串
writerline參數能夠是字符串,也能夠是字符序列app

# writelines表示寫入不少行,參數能夠是list格式
l=["i","love","you"]
with open(r'xiaoxiongceshi01.txt','a')as f:
    f.write("死了毒藥愛,\n我愛你\n")
    f.writelines("生活不只有眼前的苟且\n")
    f.writelines("還要遠方的枸杞\n")
    f.writelines(l)

持久化 - pickle

序列化(持久化,落地):把程序運行中的信息保存在磁盤上
反序列化: 序列號的逆過程
pickle: python提供的序列化模塊
pickle.dump:序列化
pickle.load:反序列化svg

# 序列化案例
import pickle
age = [15145,'sad',''sdadasd'']
with open(r'test01.txt', 'wb') as f:
    pickle.dump(age, f)
# 反序列化案例
import pickle
with open(r'test01.txt', 'rb') as f:
    age = pickle.load(f)
    print(age)=>[15145,'sad',''sdadasd'']

持久化-shelve

持久化工具
相似字典,用kv對保存數據,存取方式跟字典也相似
open, close函數

# 打開文件
import shelve
shv=shelve.open(r'shv.db')
shv['one']=1
shv['two']=2
shv['three']=3
shv.close()
# shelve讀取案例
import shelve
shv = shelve.open(r'shv.db')
try:
    print(shv['one'])
    print(shv['threee'])
except Exception as e:
    print("煩死了")
finally:
    shv.close()
# shelve 使用with管理上下文環境,不用寫try和close了
# shelve忘記寫回,須要使用強制寫回
import shelve
with shelve.open(r'shv.db', writeback=True) as shv:
    shv['one'] = {"eins":1, "zwei":2, "drei":3}
    k1 = shv['one']
    print(k1)=>{'eins': 1, 'zwei': 2, 'drei': 3}
    # 此時,一旦shelve關閉,則內容仍是存在於內存中,沒有寫回數據庫
    k1["eins"] =1000

with shelve.open(r'shv.db') as shv:
    print(shv['one'])=>{'eins': 1000, 'zwei': 2, 'drei': 3}