深淺拷貝&文件操作
阿新 • • 發佈:2019-04-07
列表 讀寫 報錯 鏈接 fas strip() 元素 txt文件 文件操作
一.深淺拷貝
淺拷貝:拷貝第一層(頂級對象),或者說:父級對象
深拷貝:拷貝所有對象,頂級對象及其嵌套對象,或者說父級對象及其子對象
# 應該每次都拷貝一份(但由於小數據池,未拷貝) v1 = ‘alex‘ import copy v2 = copy.copy(v1) print(id(v1),id(v2)) #同一地址
練習1 impot copy v1 = [1,2,3] v2 = copy.copy(v1) #淺拷貝 print(v1 == v2) # True print(v1 is v2) # False print(v1[0] is v2[0]) # True
練習2
import copy
v1 = [1,2,3,{"name":‘武沛齊‘,"numbers":[7,77,88]},4,5]
v2 = copy.copy(v1)
print(v1 is v2) #False
print(v1[0] is v2[0]) #True
print(v1[3] is v2[3]) #True
print(v1[3][‘name‘] is v2[3][‘name‘]) #True
print(v1[3][‘numbers‘] is v2[3][‘numbers‘]) #Trueprint(v1[3][‘numbers‘][1] is v2[3][‘numbers‘][1]) #True
練習3 import copy v1 = [1,2,3,{‘k1‘:123,‘k2‘:456}] v2 = copy.deepcopy(v1) print(v1 == v2) # True print(v1 is v2) # False print(v1[0] is v2[0]) # True print(v1[3] == v2[3]) # True print(v1[3] is v2[3]) # False
練習四
import copyv1 = [1,2,3,{"name":‘武沛齊‘,"numbers":[7,77,88]},4,5] v2 = copy.deepcopy(v1) print(v1 is v2) #False print(v1[0] is v2[0]) #True print(v1[3] is v2[3]) #False print(v1[3][‘name‘] is v2[3][‘name‘]) #True print(v1[3][‘numbers‘] is v2[3][‘numbers‘]) #False print(v1[3][‘numbers‘][1] is v2[3][‘numbers‘][1]) #True
二.文件操作
- 讀取: r (read):只能讀不能寫,文件不存在就報錯
#打開文件: object = open(‘某txt文件‘,mode = ‘r‘,encoding = ‘utf-8‘) #讀取內容: content = object.read() print(content) #關閉內容: object.close()
2. 寫入:w (write): 只能寫不能讀(先清空文件),文件不存在就新建
#打開文件: object = open(‘某個txt文件或要新建的文件‘,mode = ‘w‘,encoding = ‘utf-8‘) #寫內容: object.write(‘xxx‘) #關閉文件: object.close()
3.追加; a (append): 只能追加不能讀,不存在則新建
#打開文件: object = open (‘某個txt文件或要新建的文件‘,mode = ‘a‘,encoding = ‘utf-8‘) #寫內容: object.append() #關閉文件 object.close()
三.文件可讀寫:
1.可讀可寫
讀取 #寫入:根據光標的位置,從當前光標位置開始進行寫入操作(可能會將其他的文字覆蓋) file_object = open(‘某個txt文件‘,mode=‘r+‘,encoding=‘utf-8‘) file_object.seek(2) # 調整光標的位置(根據字節來調整) #讀取內容: content = file_object.read() print(content) file_object.write(‘xxx‘) #關閉文件: file_object.close()
2.可讀可寫
object = open (‘某個txt文件‘,mode = ‘w+‘,encoding = ‘utf-8‘) data = object.read() print(data) object.write(‘xxx‘) object.seek(0) data = object.read() print(data) file_object.close()
四.讀寫操作
1.讀:
file_object = open(‘某個txt文件‘,mode=‘r‘,encoding=‘utf-8‘) # 讀取文件的所有內容到內存 con = file_object.read() #從當前光標所在的位置向後讀取文件兩個字符 con = file_object.read(2) # 讀取文件的所有內容到內存,並按照每一行進行分割到列表中。 data_list = file_object.readlines() # 如果以後讀取一個特別大的文件 (**********) for line in file_object: line = line.strip() # strip : 去除換行(\n) print(line) #一行一行進行讀取
2.寫:
file_object = open(‘log.txt‘,mode=‘w‘,encoding=‘utf-8‘) file_object.write(‘asdfadsfasdf\n‘) file_object.write(‘asdfasdfasdfsadf‘) file_object.close()
五.練習:
1.請將user中的元素根據 _ 鏈接,並寫入 ‘a1.txt‘ 的文件
user = [‘alex‘,‘eric‘] data = ‘_‘.join(user) object = open(‘a1.txt‘,mode = ‘w‘,encoding = ‘utf-8‘) object.write(data) object.close()
2.請將user中的元素根據 | 鏈接,並寫入 ‘a2.txt‘ 的文件
user = [ {‘name‘:‘alex‘,‘pwd‘:‘123‘}, {‘name‘:‘eric‘,‘pwd‘:‘olbody‘},] object = open(‘a2.txt‘,mode = ‘w‘,encoding = ‘utf-8‘) for item in user: line = ‘%s|%s\n‘ %(item[‘name‘],item[‘pwd‘]) object.write(line) object.close()
3.請將a2.(根據2)txt中的文件讀取出來並添加到一個列表中 [‘alex|123‘,‘eric|olbody‘]
方法一: list = [] objecct = open(‘a2.txt‘,mode = ‘r‘,encoding = ‘utf-8‘) for line in object: line = line.strip() list.append(line) print(list) 方法二: file_obj = open(‘a2.txt‘,mode=‘r‘,encoding=‘utf-8‘) content = file_obj.read() file_obj.close() content = content.strip() #去除最後一個換行 data_list = content.split(‘\n‘) #根據中間的換行進行切割 print(data_list)
深淺拷貝&文件操作