深浅拷贝&文件操作
程序员文章站
2022-06-13 15:26:40
一.深浅拷贝 浅拷贝:拷贝第一层(*对象),或者说:父级对象 深拷贝:拷贝所有对象,*对象及其嵌套对象,或者说父级对象及其子对象 二.文件操作 2. 写入:w (write): 只能写不能读(先清空文件),文件不存在就新建 3.追加; a (append): 只能追加不能读,不存在则新建 三.文 ......
一.深浅拷贝
浅拷贝:拷贝第一层(*对象),或者说:父级对象
深拷贝:拷贝所有对象,*对象及其嵌套对象,或者说父级对象及其子对象
# 应该每次都拷贝一份(但由于小数据池,未拷贝) 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']) #true
print(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 copy v1 = [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)
上一篇: 解析PHP的session过期设置