I. Deep and Shallow Copies
Shallow copy: Copy the first layer (top-level object), or parent object
Deep copy: Copy all objects, top-level objects and their nested objects, or parent objects and their children
# Copy one copy at a time (but not because of small datapools) v1 = 'alex' import copy v2 = copy.copy(v1) print(id(v1),id(v2)) #Same address
Exercise 1 impot copy v1 = [1,2,3] v2 = copy.copy(v1) #shallow copy print(v1 == v2) # True print(v1 is v2) # False print(v1[0] is v2[0]) # True
Exercise 2
import copy
v1 = [1,2,3,{"name":'Wu Po Qi',"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
Exercise 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
Exercise four
Import import import copy V1 = import copy V1 = [1,2,3, {name":'Wupei Qi', {number": [7,77,88]]], 4,5] V2 = copy. deepcopy (v1) print (v1 is v2) # False print (v1 [0] is V2 [0]] # True print (v1 [0]]] #True print (v1 [v1 [3] is V2 [3] is V2 [3]] #False print (v1 [v1 [3]]['v1 [3]] ['v1]]['name] \fonfonfonfonfonfonfonfonfonfonfonfonfonfonfonfonfonfon3]['numbers'] # False print (v1 [3]['numbers'] [1] is V2 [3]['numbers'] [1]) # True
II. File Operation
- Read: r (read): Can only read but not write, if the file does not exist, it will report an error.
#Open file: object = open('some txt file',mode = 'r',encoding = 'utf-8') #Read content: content = object.read() print(content) #Close content: object.close()
2. Write: w (write): Write but not read (empty the file first), create a new file if it does not exist
#Open file: object = open('Certain txt Documents or files to be created',mode = 'w',encoding = 'utf-8') #Writing content: object.write('xxx') #Close file: object.close()
3. Added; a (append): Added only, unreadable, new if nonexistent
#Open file: object = open ('Certain txt Documents or files to be created',mode = 'a',encoding = 'utf-8') #Writing content: object.append() #Close file object.close()
3. Documents are readable and writable:
1. Readable and Writable
read #Write: Write from the current cursor position, depending on the cursor position (other text may be overwritten) file_object = open('Certain txt file',mode='r+',encoding='utf-8') file_object.seek(2) # Adjust the position of the cursor(Adjust according to bytes) #Read content: content = file_object.read() print(content) file_object.write('xxx') #Close file: file_object.close()
2. Readable and Writable
object = open ('Certain txt file',mode = 'w+',encoding = 'utf-8') data = object.read() print(data) object.write('xxx') object.seek(0) data = object.read() print(data) file_object.close()
4. Read and write operations
1. reading:
file_object = open('Certain txt file',mode='r',encoding='utf-8') # Read all the contents of the file into memory con = file_object.read() #Read back two characters of the file from the current cursor position con = file_object.read(2) # Read all the contents of the file into memory and divide them into lists according to each line. data_list = file_object.readlines() # If you read a particularly large file later (**********) for line in file_object: line = line.strip() # strip : Remove line break(\n) print(line) #Read line by line
2. write:
file_object = open('log.txt',mode='w',encoding='utf-8') file_object.write('asdfadsfasdf\n') file_object.write('asdfasdfasdfsadf') file_object.close()
Five. Practice:
1. Please link the elements in user according to _link and write them to the file'a1.txt'.
user = ['alex','eric'] data = '_'.join(user) object = open('a1.txt',mode = 'w',encoding = 'utf-8') object.write(data) object.close()
2. Please link the elements in user according to | and write to the file'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. Please read out the files in a2. (2)txt and add them to a list ['alex|123','eric|olbody']
Method 1: list = [] objecct = open('a2.txt',mode = 'r',encoding = 'utf-8') for line in object: line = line.strip() list.append(line) print(list) //Method two: file_obj = open('a2.txt',mode='r',encoding='utf-8') content = file_obj.read() file_obj.close() content = content.strip() #Remove the last newline data_list = content.split('\n') #Cut according to the line change in the middle print(data_list)