1. 程式人生 > 實用技巧 >python ( 進階 第一部 )

python ( 進階 第一部 )

目錄

列表的相關操作與函式

字串的相關操作與函式

集合相關操作與函式

字典相關操作與函式

深淺拷貝

檔案操作

列表的相關操作

列表的拼接

lst1 = [1,2,3]
lst2 = [4,5,6,6]
res = lst1 + lst2
print(res)

列表的重複

res = lst1 * 3
print(res)

列表的切片

語法: [開始索引:結束索引:間隔值]

[:] 或 [::] 擷取所有列表

列表的修改

lst = ["呂洞賓","何仙姑","鐵柺李","曹國舅","張果老","藍采和","韓湘子","王文"]
lst[3:5] = ["往返","晏國彰
","牧樹人"] print(lst)

利用切片可以一次修改多個元素,沒有數量上的限制

切片匹配和步長(間隔值),切出多上個元素,修改多少個元素.

列表的刪除

lst = [“呂洞賓”,“何仙姑”,“鐵柺李”,“曹國舅”,“張果老”,“藍采和”,“韓湘子”,“王文”]
del lst[-1]
print(lst)
#刪除列表的元素

lst = [“呂洞賓”,“何仙姑”,“鐵柺李”,“曹國舅”,“張果老”,“藍采和”,“韓湘子”,“王文”]
res = lst[-1]
del res
print(lst)
#刪除的是變數res本身,不是列表中的元素

列表的相關函式

lst = ['
123"] #1 .append 向列表的末尾追加一個新的元素 lst.append('你好') print(lst) #2 .insert 自定索引新增元素 lst.insert(0,'流雲') print(lst) #3 .extend 迭代追加所有元素 lstvar= '99' lst.extend(lstvar) print(lst)

# pop 通過索引刪除元素,若沒有索引預設移除最後一個元素
lst = ["晏國彰","毛洪磊","劉子濤","劉聰","牧樹人"]
res = lst.pop(1)
res = lst.pop()
print(res)
print(lst)


# remove 通過給定的元素進行刪除,如果有多個元素,預設刪除第一個 lst = ["晏國彰","毛洪磊","劉子濤","劉聰","牧樹人","劉子濤","劉子濤"] lst.remove("劉子濤") print(lst) # clear 清空列表 lst = ["晏國彰","毛洪磊","劉子濤","劉聰","牧樹人","劉子濤","劉子濤"] lst.clear() print(lst)

改, 查 ( 參考列表的相關操作 )

列表的其他函式

index 獲取某個值在列表中的索引

列表.index(值[start][end]) #[ ] 表達引數可選項,找不到報錯

lst = ["晏國彰","毛洪磊","劉子濤","劉聰","牧樹人","劉子濤","劉子濤"]
res = lst.index("劉子濤")
res = lst.index("劉子濤",3)
res = lst.index("劉子濤",3,5) error
print(res)

count 計算某個元素出現的次數(不可劃分範圍)

lst = ["晏國彰","毛洪磊","劉子濤","劉聰","牧樹人","劉子濤","劉子濤"]
res = lst.count("劉子濤")
print(res)

sort() 列表排序

從小到大排列
lst = [44,99,1,10,3,-5,-90]
lst.sort()
從大到小排列
lst.sort(reverse=True)
print(lst)

reverse() 列表反轉

reverse() 列表反轉操作
lst = ["王文","劉德華","郭富城","張學友"]
lst.reverse()
print(lst)

字串的相關操作

字串的拼接

strvar = "今天是" + "星期一"
strvar += ",今天非常開心"
print(strvar)

字串的跨行拼接

strvar = "sdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdf" \
"多餘的幾行放在第二行進行顯示"
print(strvar)

字串的重複

strvar = "重要的事情說三遍" * 3
print(strvar)

字串的切片

語法: 字串{::]完整格式: [start:end:間隔值]

[:] 或[::] 擷取所有字串

formate 填充符的使用 ( ^<>)

1. ^原字串居中 >原字串居右, <原字串居左

2. {who:^10}

  who:關鍵字引數,:要填充的字元,^:原字串居中

  10:總長度 = 原字串長度 + 填充字串長度

strvar = "{who:*^10}在{where:>>10},{do:!<10}".format(who="劉鵬",where="電影院",do="拉屎")
print(strvar)

3. 特殊站位符的使用( :d :f :s ,:)

::d 整型站位符(要求型別必須是整型)

strvar = "劉子豪昨天買了{:d}個花露水".format(100) # 100.5error
print(strvar)

# :2d 佔用倆位, 不夠倆位空格來補,預設居右
strvar = "劉子豪昨天買了{:2d}個花露水".format(3)

:f 浮點數佔位符(要求必須是浮點數)

strvar = "劉心畢業時,找工作的薪資是{:f}".format(2.5)

#:2f  小數點保留倆位

strvar = "劉心畢業時,找工作的薪資是{:.2f}".format(2.56789)
print(strvar)

:s 字串佔位符 (要求必須是,字串)

strvar = "{:s}".format("今天天氣不錯,萬里無雲")
print(strvar)

:, 金錢站位符

strvar = "{:,}".format(123456789)
print(strvar)

綜合案例:

strvar = "{}向{}開了一槍,銀彈而亡".format("李志輝","明浩")
print(strvar)

字串的格式化 formate

順序傳參

strvar = "{}向{}開了一槍,銀彈而亡".format("李志輝","明浩")
print(strvar)

索引傳參

strvar = "考試時{1},遊戲時{0}".format("唯唯諾諾","重拳出擊")
print(strvar)

關鍵字傳參

strvar = "{who2}甩了一個飛吻,{who1}神魂顛倒".format(who1="劉彩霞",who2="馬生平")
print(strvar)

容器型別資料(列表元組)傳參

strvar = "{1[2]}向{0[0]}拋了一個媚眼,鼻血直冒三萬多尺,失血而亡".format(["孫翔群","曹晨光","宋雲傑"],("李亞","孫致和","溫子月"))
print(strvar)

format當中,不可以使用逆向下標,不識別

strvar = "{group2[0]}向{group1[-1]}拋了一個媚眼,鼻血直冒三萬多尺,失血而亡".format(group1 = ["孫翔群","曹晨光","宋雲傑"],group2 = ("李亞","孫致和","溫子月"))
print(strvar)

如果容器是字典,直接寫值,不需要加上引號

strvar = "{group1[ccg]}向{group2[1]}拋了一個媚眼,鼻血直冒三萬多尺,失血而亡".format(group1 = {"kxq":"孫翔群","ccg":"曹晨光","syj":"宋雲傑"},group2 = ("李亞","孫致和","溫子月"))
print(strvar)

字串的相關函式

capitalize 每個單詞的首字母大寫

strvar = "how old are you"
res = strvar.capitalize()
print(res)

title 句首字母大寫

strvar = "how old are you"
res = strvar.capitalize()
print(res)

upper 所有字母都大寫

strvar = "to be or not to be that is a question"
res = strvar.upper()
print(res)

lower 所有字母都小寫

strvar = "to be or not to be that is a question"
res = strvar.lower()
print(res)

swapcase 大小寫互換

strvar = "I Love You"
res = strvar.swapcase()
print(res)

len 計算字串長度

strvar = "adfs234sdfsa"
res = len(strvar)
print(res)

count 統計字串某個元素的數量

strvar = "adfs234sdfsa"
res = strvar.count("a")
print(res)

find 查詢某個元素第一次出現的索引位置 (推薦使用) 字串.find('元素':start:end) 如果找不到返回-1

strvar = "oh Father this is my Favorate dog"
res = strvar.find("F")
res = strvar.find("F",4)
res = strvar.find("Fav",5,10) # 結束索引本身取不到,取到之前的那個值
print(res)

index與find功能相同 .find找不到直接報錯

res = strvar.index("Fav",5,10) error

startswith 判斷是否以某個元素開頭 字串.startswith('元素',start ,end ) 存在返回true 否則返回F安樂死

strvar = "oh Father this is my Favorate dog"
res = strvar.startswith("oh")
res = strvar.startswith("this",10)
res = strvar.startswith("this",10,13) # 10 11 12
print(res)

endswith 判斷是否以某個元素結尾

res = strvar.endswith("dog")
res = strvar.endswith("rate",-12)
res = strvar.endswith("rate",-12,-4)
print(res)

isupper 判斷 字串是否都是大寫

res = strvar.isupper()
print(res)

islower 判斷字串是否都是小寫

strvar = "abcdd12345"
res = strvar.islower()
print(res)

isdecimal 判斷字串是否是純數字

strvar = "12354"
strvar = "12354.8979112"
res = strvar.isdecimal()
print(res)

ljust 填充字串,原字串居左 (預設填充空格)

strvar = "abc"
res = strvar.ljust(10)
print(res)

rjust 填充字串,原字串居右 (預設填充空格)

strvar = "abc"
res = strvar.rjust(10,"&")
print(res)

center 填充字串,原字串居中 (預設填充空格)

strvar = "abc"
res = strvar.center(10) # 原字串長度 + 填充字元長度 = 10 ,預設填充空格
res = strvar.center(10,"#")
print(res)

strip 預設去掉收尾倆邊的空白符

strvar = "@@@@@ 周杰倫           @@@@@"
res = strvar.strip()
res = strvar.strip("@") # 指定去掉的符號
print(res)

strvar = "@@@@@ 周杰倫           @@@@@"
#rstrip 去掉右邊某個字元 
print(  strvar.rstrip("@")   )
#lstrip 去掉左邊某個字元 
print(  strvar.lstrip("@")   )

split 按某字元將字串分割成列表(預設分割字元是空格)

strvar = "you can you up no can no bb"
lst = strvar.split()
strvar = "you-can-you-up-no-can-no-bb"
lst = strvar.split("-")   # 從左到右分隔
lst = strvar.rsplit("-",2)# 從右到左分隔,(可以指定分隔的次數)
print(lst)

join 按某字元將列表拼接成字串(容器型別的都可以)

lst = ['you', 'can', 'you', 'up', 'no', 'can', 'no', 'bb']
res = "-".join(lst)
print(res)

replace 替換 ,把字串的舊字元換成新字元 replace(要替換的元素,替換成的元素,替換的次數)

strvar = "可愛的小青蛙喜歡吃蚊子,有沒有,有沒有,還有沒有"
res = strvar.replace("有沒有","真沒有")
res = strvar.replace("有沒有","真沒有",1)
print(res)

集合相關操作與函式

集合相關的操作

intersection() 交集 &

set1 = {"jacklove","theshy","rookie","xboyww"}
set2 = {"倪萍","張國榮","趙本山","劉能","趙四","xboyww"}
res = set1.intersection(set2)
print(res)
簡寫 &
res = set1 & set2
print(res)

difference() 差集 -

set1 = {"jacklove","theshy","rookie","xboyww"}
set2 = {"倪萍","張國榮","趙本山","劉能","趙四","xboyww"}
res = set1.difference(set2)
print(res)
簡寫
res = set1 - set2
print(res)

union() 並集 |

set1 = {"jacklove","theshy","rookie","xboyww"}
set2 = {"倪萍","張國榮","趙本山","劉能","趙四","xboyww"}
res = set1.union(set2)
print(res)
簡寫 |
res = set1 | set2
print(res)

symmetric_difference() 對稱差集

set1 = {"jacklove","theshy","rookie","xboyww"}
set2 = {"倪萍","張國榮","趙本山","劉能","趙四","xboyww"}
res = set1.symmetric_difference(set2)
print(res)
簡寫 ^
res = set1 ^ set2
print(res)

isubset() 判斷是否是子集

set1 = {"周杰倫","王力巨集","羅志祥","潘瑋柏"}
set2 = {"周杰倫","王力巨集"}
res = set1.issubset(set2)
print(res)
簡寫 < 
res = set1 < set2
print(res)

issuperset() 判斷是否是父集

set1 = {"周杰倫","王力巨集","羅志祥","潘瑋柏"}
set2 = {"周杰倫","王力巨集"}
res = set1.issuperset(set2)
print(res)
簡寫
res = set1 > set2
print(res)

is第三joint() 檢測倆個集合是否不相交

set1 = {"周杰倫","王力巨集","羅志祥","潘瑋柏"}
set2 = {"周杰倫","王力巨集"}
res = set1.isdisjoint(set2)
print(res)

集合相關函式

.add( 向集合中新增元素( 一次加一個)

setvar = {"神祕男孩","金角大王"}
setvar.add("銀角大王")
print(setvar)

.update() 迭代新增元素( 一次加一堆)

setvar = {"神祕男孩","金角大王"}
strvar = ("劉子豪","劉鑫")
setvar.update(strvar)
print(setvar)

pop() 隨機刪除集合中的一個元素

setvar = {"神祕男孩","金角大王"}
res = setvar.pop()
print(res , setvar)

clear() 清空集合

setvar = {"神祕男孩","金角大王"}
setvar.clear()
print(setvar)

remove() 刪除集合中指定的值( 不存在時會報錯) 瞭解

strvar = ("劉子豪","劉鑫")
setvar.remove("神祕男孩")
print(setvar)

discard() 刪除集合中指定的值( 不存在時不報錯) 推薦使用

setvar.discard("神祕男孩")
setvar.discard("神祕男孩1234324")
print(setvar)

.for怎set() 可強轉容器型別資料變為冰凍集合 冰凍集合一旦建立,不能進行任何修改,只能做交差並補操作

lst1 = ["王聞",18,"男性","愛好:跑步"]
fz1 = frozenset(lst1)

lst2 = ("王聞","地址:包頭","買一輛特斯拉9.9包郵")
fz2 = frozenset(lst2)
print(fz1 , type(fz1))
print(fz2 , type(fz2))
#不能夠在冰凍集合當中新增或者刪除元素
#fz1.add(123) error
#只能交差並補
print(  fz1 & fz2 )
print(  fz1 - fz2 )

字典相關操作與函式

方式一:

dic = {}
dic[“ww”] = “一表人才,除了帥氣的皮囊之外,一無所有”
dic[‘ywz’] = “渣男”
dic[“hxl”] = “摳腳老漢”
print(dic)

方式二:

fromkeys() 使用一組鍵和預設值建立字典
lst = ["ww","ywz","hxl"]
dic = {}.fromkeys(lst,None)
print(dic)

注意點: 三個鍵所指向的列表是同一個(不推薦使用方式二)

lst = ["ww","ywz","hxl"]
dic = {}.fromkeys(lst,[])
print(dic)
dic["ww"].append(1)
print(dic)

dic["ww"] = []
dic['ywz'] =[]
dic["hxl"] =[]

pop( ) 通過鍵去刪除鍵值對( 若沒有該鍵可設定預設值,預防報錯)

dic = {"top":"花木蘭" , "middle":"甄姬" , "bottom":"孫尚香" ,  "jungle" : "鍾馗" , "support":"蔡文姬" }
res = dic.pop("top")
# 如果刪除的是不存在的鍵,直接報錯
# res = dic.pop("top123")
# 可以設定預設值,防止報錯
# res = dic.pop("top123","沒有這個鍵")
res = dic.pop("middle","沒有這個鍵")

popitem() 刪除最後一組鍵值對

dic = {"top":"花木蘭" , "middle":"甄姬" , "bottom":"孫尚香" ,  "jungle" : "鍾馗" , "support":"蔡文姬" }
res = dic.popitem()
print(res, dic)

clear(() 清空字典

dic.clear()

print(dic)

#update() 批量更新(有該鍵就更新,沒該鍵就新增)
dic = {'ww': '一表人才,除了帥氣的皮囊之外,一無所有', 'ywz': '渣男', 'hxl': '摳腳老漢'}
dic_new = {"ywz":"暖男","hxl":"扣手","ly":"小美女,單純,可愛,活潑,靈巧...."}

# 方法一(推薦)
dic.update(dic_new)
print(dic)

# 方法二
dic.update(kxq="聰明,活潑,可愛,靈巧,惹人喜歡",ccg="樂觀,大方,可愛,靈巧")
print(dic)

#get()    通過鍵獲取值(若沒有該鍵可設定預設值,預防報錯)
dic = {'ww': '一表人才,除了帥氣的皮囊之外,一無所有', 'ywz': '暖男', 'hxl': '扣手'}
res = dic.get("ww")
res = dic.get("zbcdefg") # None
res = dic.get("zbcdefg","沒有這個鍵")
# res = dic['abcee'] error
print(res)

字典重點函式

dic = {'ww': '一表人才,除了帥氣的皮囊之外,一無所有', 'ywz': '暖男', 'hxl': '扣手'}
#keys()   將字典的鍵組成新的可迭代物件
res = dic.keys()
print(res)

#values() 將字典中的值組成新的可迭代物件
res = dic.values()
print(res)

#items()  將字典的鍵值對湊成一個個元組,組成新的可迭代物件 
res = dic.items()
print(res)

深淺拷貝

a = 15
b = a
a = 16
print(b)

lst1 = [1,2,3]
lst2 = lst1
lst1.append(4)
print(lst2)



#淺拷貝
import copy
lst1 = [1,2,3]
# 方法一 copy.copy 模組.方法
lst2 = copy.copy(lst1)

lst1.append(4)
print(lst1)
print(lst2)


# 方法二  列表.copy()
lst1 = [1,2,3,4]
lst3 = lst1.copy()
lst1.insert(0,0)
print(lst1)
print(lst3)



#深拷貝
import copy
lst1 = [1,2,3,[4,5,6]]
lst2 = copy.copy(lst1)
lst1[-1].append(7)
lst1.append(100)
print(lst1)
print(lst2)

lst1 = [1,2,3,[4,5,6]]
lst2 = copy.deepcopy(lst1)
# lst1[-1].extend("ab")
print(lst2)
print(lst1)
print(id(lst2[-1]))
print(id(lst1[-1]))

print(id(lst1[0]))
print(id(lst2[0]))

lst2[0] = 1111
print(id(lst1[0]))
print(id(lst2[0])

總結:

(1) 淺拷貝只拷貝一級容器的所有資料

深拷貝拷貝所有層級的所有資料

淺拷貝速度比深拷貝速度快

深拷貝在執行時,如果是不可變資料,地址會暫時的指向原來資料

如果是可變資料,直接開闢新的空間

不可變資料 Number str tuple

可變資料 ;list set dic

檔案操作

格式:

fp = open (“檔名”,node = “模式”,encoding=‘utf-8")
fp->檔案的io物件(檔名柄)
i => input 輸入
o => output 輸出

檔案的寫入

# 1.開啟檔案
fp = open("ceshi1.txt",mode="w",encoding="utf-8") # 

# 2.寫入內容
fp.write("把大象塞進去") # 把大象放進去

# 3.關閉檔案
fp.close() # 把冰箱門關上

檔案的讀取

# 1.開啟檔案
fp = open("ceshi1.txt",mode="r",encoding="utf-8") 

# 2.讀取內容
res = fp.read() # 把大象拿出來
print(res)

# 3.關閉檔案
fp.close() # 把冰箱門關上

位元組流的轉換

1. bytes:是用來傳輸或者儲存的資料格式 b’1234’ b"abcd" b"我愛你" -> b開頭的位元組流,範圍只能是ascii編碼

2. 如果是中文使用encode 和 decode 來進行轉換 將字串和位元組流(Bytes流)型別進行轉換 (引數寫成轉化的字元編碼格式)

3. encode() 編碼將字串轉化為位元組流(bytes流)

4. decode() 解碼 將bytes流轉化為字串

5. a = b'1234'      print(a,type(a))

strvar = "我愛你"
# encode 編碼 -> 變成二進位制位元組流
res = strvar.encode("utf-8")
print(res , type(res))

# decode 解碼 =>二進位制位元組流恢復成原來的字串
res2 = res.decode("utf-8")
print(res2 , type(res2))

6. len可以計算位元組個數

  num= len(res)

  printnum)

7. 一箇中文佔用3個位元組,通過decode 反解出愛這個自

  res3 =b"\xe7\x88\xb1".decode(“utf-8”)

  print(res3)

8. 例題:

#  程式設計師的表白方式
strvar = "我!是你一輩子也得不到的男人"
strvar2 = strvar.encode()
print(strvar2)

# 四.儲存二進位制位元組流
"""不需要指定encoding編碼集,否則報錯"""
fp = open("ceshi2.txt",mode="wb")
fp.write(strvar2)
fp.close()

# 五.讀取二進位制位元組流
fp = open("ceshi2.txt",mode="rb")
res = fp.read()
fp.close()

print(res)
# 通過decode反解出字串
strvar = res.decode()
print(strvar)

# 六.複製圖片
"""圖片,音訊,視訊"""
# 1.讀取原圖片所有的內容
fp = open("集合.png",mode="rb")
res = fp.read()
fp.close() 

# 2.把讀取的內容儲存到另外一個檔案
# fp = open("集合2.png",mode="wb")
# 指定絕對路徑(完整路徑)
fp = open(r"E:\python31\day8\集合3.png",mode="wb")
fp.write(res)
fp.close()

檔案的擴充套件模式

1. (utf-8編碼格式下 預設一箇中文佔三個位元組,一個英文符號,佔用一個位元組)

  read() 功能 : 讀取字元的個數(裡面的引數代表字元個數)

  seek() 功能 調整指標的位置(裡面的引數代表位元組的個數)

  tell() 功能 當前游標左側所有的位元組數

2. seek(0) 直接把游標移動到檔案開頭

 seek(0,2) 直接把游標移動到檔案末尾

r+模式

# r+ 先讀後寫
fp = open("ceshi3.txt",mode="r+",encoding="utf-8") 
# 先讀
res = fp.read()
print(res)

# 後寫
fp.write("1234")

# 在讀
fp.seek(0) # 調整游標位置在開頭
res = fp.read()
print(res)
fp.close()


# r+ 先寫後讀
fp = open("ceshi3.txt",mode="r+",encoding="utf-8")
fp.seek(0,2) # 調整游標位置在末尾
fp.write("123"

fp.seek(0)
res = fp.read()
print(res)
fp.close()

w+模式

fp = open("ceshi4.txt",mode="w+",encoding="utf-8")
fp.write("abc")


fp.seek(0)
res = fp.read()
print(res)
fp.close()

a+模式

fp = open("ceshi5.txt",mode="a+",encoding="utf-8")
fp.write("123")

fp.seek(0)
res = fp.read()
print(res)

# a模式在寫入內容時,會強制把游標移動到最後
fp.seek(1)
fp.write("abc")
fp.close()

4. 如果r模式內,區別a模式

fp = open("ceshi5.txt",mode="r+",encoding="utf-8")
fp.seek(1)
fp.write("abc")
fp.close()

5. read seek tell 三個函式的使用

fp = open("ceshi6.txt",mode="r+",encoding="utf-8")
res = fp.read(3)
print(res)

fp.seek(6)
print(fp.read(1))

6. 計算檔案指標在左側所有的位元組數

res = fp.tell()
print(res)
fp.close()

7. 注意點: seek 移動中文位元組的時候,有可能報錯

fp = open("ceshi6.txt",mode="r+",encoding="utf-8")
fp.seek(2)
res = fp.read()
print(res)
fp.close()

# print("我".encode())
# b'\xe6\x88\x91'

8. with語法的使用 ( close 操作with語法可以自動實現)

with open("集合.png",mode="rb") as fp:
 res = fp.read()

with open(r"E:\python31\day8\集合4.png",mode="wb") as fp:
 fp.write(res)

with open(r"E:\python31\day8\集合4.png",mode="wb") as fp:
 fp.write(res)

檔案關閉的意義

1,. 重新整理快取區 flush

  當檔案關閉的時候自動重新整理快取區

  當整個程式執行結束的時候自動重新整理快取區

  當快取區寫滿了 ,會自動重新整理快取區

2. 手動重新整理快取區

fp = open("ceshi6.txt",mode="r+",encoding="utf-8")
fp.write("zzz")
# 手動把緩衝區裡面的內容寫入檔案當中
fp.flush()
while True:
    pass
fp.close()

3. 檔案的相關函式

fp = open(“ceshi6.txt”,mode=“a+”,encoding=“utf-8”)
#readable() 功能: 判斷檔案物件是否可讀
res = fp.readable()
print(res)
#writable() 功能: 判斷檔案物件是否可寫
res = fp.writable()
print(res)

4. readine() 功能: 讀取一行檔案內容

  引數> 當前行字元總個數=> 以當前行讀取

  引數< 當前行字元總個數=> 以引數的大小來讀取字元的個數

5. 預設讀取readine 讀取一行

"""
with open("ceshi7.txt",mode="r+",encoding="utf-8") as fp: 
 # res = fp.readline(3000)
 # print(res) 
 # 檔案物件fp也是一個可迭代物件
 '''在遍歷檔案物件的時候,預設一次拿一行'''
 for i in fp:
  print(i)
# 讀取所有內容
with open("ceshi7.txt",mode="r+",encoding="utf-8") as fp: 
 # 先讀取一行
 res = fp.readline()
 # 判斷是不是空
 while res:
  print(res)
  res = fp.readline()
"""
#readlines()    功能:將檔案中的內容按照換行讀取到列表當中
lst_new = []
with open("ceshi7.txt",mode="r+",encoding="utf-8") as fp: 
 lst = fp.readlines()
 # print(lst) # ['\t窗前明月光\n', '疑是鞋兩雙\t\t\n', '\t\t舉頭王明月\n', '\t低頭看褲襠']
 for i in lst:  
  lst_new.append(i.strip())
print(lst_new)  # ['窗前明月光', '疑是鞋兩雙', '舉頭王明月', '低頭看褲襠']
#writelines()   功能:將內容是字串的可迭代性資料寫入檔案中 引數:內容為字串型別的可迭代資料
"""可迭代型資料(容器型別資料,range物件,迭代器)"""
"""
lst = ["春眠不覺曉\n","處處蚊子咬\n","夜來大狗熊\n","一個也跑不了\n"]
# lst = [1,2,3,4] error
with open("ceshi8.txt",mode="w",encoding="utf-8") as fp:
 fp.writelines(lst)
"""
#truncate()     功能: 把要擷取的字串提取出來,然後清空內容將提取的字串重新寫入檔案中 (位元組)
with open("ceshi8.txt",mode="r+",encoding="utf-8") as fp:
 fp.truncate(3)
while " ":
 print(123)