openpyxl 模組學習記錄
阿新 • • 發佈:2020-12-01
一.簡介
fp = open(檔名,模式,字元編碼)
返回檔案io物件 => fp(檔案控制代碼)
i => input 輸入
o => output 輸出
檔案內容可以寫入1.字串 2.位元組流
二.檔案的操作
1.1檔案的寫入操作
# 1.開啟檔案 fp = open("lianxi.txt",mode="w",encoding="utf-8") # 開啟冰箱門 # 2.寫入檔案 fp.write("把大象塞進去") # 把大象塞進去 # 3.關閉檔案 fp.close() # 關上冰箱門
1.2檔案的讀取操作
# 1.開啟檔案 fp = open("lianxi.txt",mode="r",encoding="utf-8") # 2.讀取檔案 res = fp.read() # 3.關閉檔案 fp.close() print(res)
1.3位元組流
什麼是位元組流:
"""
位元組流語法格式(前面加b) : b"123"
二進位制位元組流的應用場景: 在資料傳輸或者在資料儲存時,使用的一種資料格式;
多個字元放在一起叫做字串,多個位元組放在一起叫做位元組流
"""
# 將字串和位元組流(Bytes流)型別進行轉換 (引數寫成轉化的字元編碼格式)
#encode() 編碼 將字串轉化為位元組流(Bytes流)
#decode() 解碼 將Bytes流轉化為字串
# 把中文變成位元組流 # encode 編碼 strvar = "我愛你".encode("utf-8") print(strvar) # decode 解碼 res = strvar.decode("utf-8") print(res) # 三個位元組表達一箇中文字元 s_bytes = b'\xe7\x88\xb1' res = s_bytes.decode("utf-8") print(res)
1.1寫入位元組流
"""mode = wb 代表寫入的是位元組流 , 不要指定任何編碼集 """ # 1.開啟檔案 fp = open("lianxi2",mode="wb") strvar= "愛上一匹野馬,家裡沒有草原" # 2.寫入位元組流 fp.write(strvar.encode()) # 3.關閉檔案 fp.close()
1.2讀取位元組流
"""mode = rb 代表讀取的是位元組流 , 不要指定任何編碼集 """ # 1.開啟檔案 fp = open("lianxi2",mode="rb") # 2.讀取位元組流 res = fp.read() # 3.關閉檔案 fp.close() print(res) print(res.decode())
1.4複製圖片和視訊
圖片:
# ### 複製圖片 # 開啟原檔案,讀取其中的位元組流 fp = open("集合.png",mode="rb") res = fp.read() fp.close() print(res) # 把這些位元組流寫入到另外檔案中 fp = open("集合2.gif",mode="wb") fp.write(res) fp.close()
視訊:
# ### 複製視訊 fp = open("ceshimv.mp4",mode="rb") res = fp.read() fp.close() print(res) fp = open("ceshimv2.mp4",mode="wb") fp.write(res) fp.close()
三 檔案的擴充套件模式
"""
# (utf-8編碼格式下 預設一箇中文三個位元組 一個英文或符號 佔用一個位元組)
#read() 功能: 讀取字元的個數(裡面的引數代表字元個數)
#seek() 功能: 調整指標的位置(裡面的引數代表位元組個數)
# seek(0) 把游標移動到檔案開頭
# seek(0,2) 把游標移動到檔案末尾
#tell() 功能: 當前游標左側所有的位元組數(返回位元組數)
"""
1.1 r+ 先讀後寫
fp = open("lianxi.txt",mode="r+",encoding="utf-8") # 先讀 res = fp.read() # 後寫 fp.write("456") # 在讀 fp.seek(0) res = fp.read() print(res) fp.close()
1.2 r+ 先寫後讀
fp = open("lianxi.txt",mode="r+",encoding="utf-8") # 先寫 fp.seek(0,2) fp.write("789") # 後讀 fp.seek(0) res = fp.read() print(res) fp.close()
1.3 w+ 可讀可寫
fp = open("lianxi2.txt",mode="w+",encoding="utf-8") # 先寫 fp.write("123") # 後讀 fp.seek(0) print(fp.read()) fp.close()
1.4 a+ 可寫可讀
fp = open("lianxi3.txt",mode="a+",encoding="utf-8") fp.write('abc') fp.seek(0) print(fp.read()) fp.close()
四 檔案的相關方法
1.1重新整理緩衝區
# ### 1.重新整理緩衝區 flush # 當檔案關閉的時候自動重新整理緩衝區 # 當整個程式執行結束的時候自動重新整理緩衝區 # 當緩衝區寫滿了 會自動重新整理緩衝區 # 手動重新整理緩衝區 """ fp = open("lianxi.txt",mode="a+",encoding="utf-8") fp.write("789") # 手動重新整理緩衝區 fp.flush() while True: pass fp.close() """
1.2檔案的相關函式
1.1 readable() 功能: 判斷檔案物件是否可讀
fp = open("lianxi.txt",mode="r",encoding="utf-8") res = fp.readable() print(res)
1.2 writable() 功能: 判斷檔案物件是否可讀
fp = open("lianxi.txt",mode="r",encoding="utf-8") res = fp.writable() print(res)
1.3readline() 功能: 讀取一行檔案內容
""" with open("lianxi.txt",mode="r",encoding="utf-8") as fp: res = fp.readline() print(res) res = fp.readline() print(res) res = fp.readline() print(res) """ # (1).先讀取一行,如果內容不是空,列印這行資料,在讀取下一行進行判斷 with open("lianxi.txt",mode="r",encoding="utf-8") as fp: res = fp.readline() while res: print(res) res = fp.readline() # (2).readline(2) 2個字元個數 """ 如果讀取個數 > 當前行總個數 : 按照當前行讀取 如果讀取個數 < 當前行總個數 : 按照個數讀取 """View Code
1.4 readlines 功能: 將檔案中的內容按照換行讀取到列表當中
with open("唐詩三百首",mode="r+",encoding="utf-8") as fp: lst = fp.readlines() print(lst) lst_new = [] for i in lst: print(i) lst_new.append(i.strip()) print(lst_new)
1.5writelines() 功能:將內容是字串的可迭代性資料寫入檔案中 引數:內容為字串型別的可迭代資料
lst_new = ['春苗不洗腳', '處處蚊子咬', '夜來大狗熊', '狼狗也來了','一個也跑不了'] with open("宋詞三百首",mode="w+",encoding="utf-8") as fp: fp.writelines(lst_new) # 在內容列表中插入一句話,叫做'狼狗也來了'放在跑不了的前面,以換行的形式插入到檔案中 lst_new = ['春苗不洗腳', '處處蚊子咬', '夜來大狗熊','一個也跑不了'] lst_new.insert(-1,"狼狗也來了") print(lst_new) lst_new2 = [] with open("王世名言",mode="w+",encoding="utf-8") as fp: for i in lst_new: lst_new2.append( i + '\n') print(lst_new2) # lst = [1,2,3] # fp.writelines(lst) error 要求是字串 fp.writelines(lst_new2)View Code
1.6 truncate() 功能:把要擷取的字串提取出來,然後清空內容將提取的字串重新寫入檔案中 (位元組)
with open("lianxi4.txt",mode="r+",encoding="utf-8") as fp: fp.truncate(9)