Document processing II

I. depth copy supplement

#Shallow copy
1,Do not change any values
l1 = [1, 2, [3, 4]]
print(l1)
print(id(l1))
print(id(l1[0]))
print(id(l1[1]))

l2 = l1.copy()
print(l2)
print(id(l2))
print(id(l2[0]))
print(id(l2[1]))
>>>[1, 2, [3, 4]]
3024156513352
140729249591552
140729249591584
[1, 2, [3, 4]]
3024306504712
140729249591552
140729249591584

2,Change value in small list
l1 = [1, 2, [3, 4]]
print(l1)
print(id(l1))
print(id(l1[0]))
print(id(l1[1]))

l2 = l1.copy()
print(l2)
print(id(l2))
l2[2][0] = 5
print(l2)
print(id(l2[0]))
print(id(l2[1]))
>>>[1, 2, [3, 4]]
2432008016968
140729249591552
140729249591584
[1, 2, [3, 4]]
2432158204936
[1, 2, [5, 4]]
140729249591552
140729249591584
//Conclusion:
    //Shallow copy except that the ID of l1 and l2 is different after copying. No matter what value is changed, other IDS are the same, because the essence is that two frameworks use the same value.
    
    
#Deep copy

import copy

l1 = [1, 2, [3, 4]]
print(l1)
print(id(l1))
print(id(l1[0]))
print(id(l1[2][0]))

l2 = copy.deepcopy(l1)
l2[0] = 66
print(l2)
print(id(l2))
print(id(l2[0]))
l2[2][0] = 5
print(id(l2[2][0]))
>>>[1, 2, [3, 4]]
1848709174344
140729247166720
140729247166784
[66, 2, [3, 4]]
1848860346184
140729247168800
140729247166848
//Conclusion:
    //Deep copy l1 is different from l2ID. Changing the value of l2 after copy will only change its own ID, but not the ID of l1
  • conclusion
Shallow copy except that the ID of l1 and l2 is different after copying. No matter what value is changed, other IDS are the same, because the essence is that two frameworks use the same value.
Deep copy l1 is different from l2ID. Changing the value of l2 after copy will only change its own ID, but not the ID of l1  

2. Text open mode

# Usage of 'r': read only. If the file is not modified, an error will be reported
f = open(r'1.txt',mode="r",encoding="utf-8")#The function of r is to prevent escaping
print(f.read())#The type of read is that the cursor runs to the end after reading, so the content will not be seen after the second printing
# print(f.read())
f.close()
>>>Ya Feng 666
//Burn it.
//Monthly salary 30k
//Monthly salary 30k
//Monthly salary 30k

#Usage of 'rb'
f = open(r'a.txt',mode='rb')#encoding = is not required at this time because it is already binary mode
print(f.read())
f.close()
>>>b'\xe6\x9c\x88\xe8\x96\xaa30k\xe5\x8a\xa0\xe6\xb2\xb9\xe5\x8a\xa0\xe6\xb2\xb9'

#Usage of 'w': write only. If not, create a new file. If the modified file exists, delete the contents of the original file first, and then write again
f = open(r'2.txt',mode='w',encoding='utf-8')
f.write("Yafeng come on, you are the best")
f.close()
>>>Yafeng come on, you are the best

#Usage of 'wb'
f = open(r'3.txt',mode='wb')#bytes type
f.write("Ya Feng 666".encode('utf-8'))#At this time, it is a string type, which needs to be changed to "wb" type, i.e. use. encode
f.close()
>>>Ya Feng 666

#Usage of'a'
f = open(r'3.txt',mode='a',encoding='utf-8')
f.write("\n Burn it.")
f.close()
>>>Ya Feng 666
   //Burn it.
    
#'r+w'Usage,#Read first and write later.
f = open(r'3.txt',mode='r+',encoding='utf-8')
print(f.read())
f.write("\n Monthly salary 30 k")
f.close()
>>>Ya Feng 666
   //Burn it.
   //Monthly salary 30k

#'r+b' is also read and write (in byte type)
f = open(r'3.txt',mode='r+b')
print(f.read())
f.write("Monthly salary 30 k".encode('utf-8'))#Write before read cursor will start from the first, that is, it will replace your original content. If the cursor is too long, it will replace all
f.close()
>>>b'\xe4\xba\x9a\xe5\xb3\xb0666\r\n\xe7\x87\x83\xe7\x83\xa7\xe5\x90\xa7\xe6\x9c\x88\xe8\x96\xaa30k'

#Usage of 'w +'
f = open('a.txt',mode='w+',encoding='utf-8')
f.write("Monthly salary 30 k")#Write before read cursor will start from the first, that is, it will replace your original content. If the cursor is too long, it will replace all
print(f.read())
f.close()
>>>Ya Feng 666
//Burning bar, 30k / month, 30k / month

#Usage of 'a +'
f = open('1.txt',mode='a+',encoding='utf-8')
f.write("\n Monthly salary 30 k")
f.seek(0)#Cursor starts at 0
print(f.read())
f.close()
>>>Ya Feng 666
//Burn it.
//Monthly salary 30k
//Monthly salary 30k
//Monthly salary 30k

//Monthly salary 30k

III. other playing methods

#read
f = open('a.txt',mode='r+',encoding='utf-8')
content = f.read(3)#All the characters read are characters. You can specify the number of reads
print(content)
f.close()
>>>Monthly salary 3

#seek
f = open('a.txt',mode='r+',encoding='utf-8')
f.seek(3)#It is to set the cursor according to the byte, and pay attention to one Chinese three characters
content = f.read()
print(content)
f.close()
>>>Salary 30 k Come on come on

#tell
f = open('a.txt',mode='a+',encoding='utf-8')
f.write('Come on')
# print(f.tell())#Tell you where the cursor is
count = f.tell()
f.seek(count-9)#Move forward 9 characters
print(f.read(2))#Read only 2 characters
f.close()
>>>Oil plus

with open(r"a.txt", mode='w')as f:
    #Note that f.write('[]') cannot be listed
    f.writelines(['1','l'])  # for + f.write#Change the mode, but the content in the list must be a string, not another type

IV. small application

  • Write text
import time

res = time.strftime('%Y-%m-%d %H:%M:%S')#Time base acquisition date, time

with open(r'01.txt','a',encoding='utf-8')as f:
    for i in range(10):
        f.write(f'{res}: It's about to learn functions. It's a little difficult. What can I do\n')
  • Get file real-time dynamic
import time

with open(r"a.txt", 'r', encoding='utf-8')as f:
    # f.seek(0, 2)  # Take the end of the file as the reference point, move 0 bits
    while True:
        # time.sleep()
        res = f.readline()  # Read one line at a time
        if res:  # If you read the data, execute the following code block
            print(f'Entry record:{res}')

Tags: Python encoding

Posted on Fri, 08 Nov 2019 09:43:10 -0500 by charliepage