Python study notes (13) --- (IO object serialization) object-oriented

Table of contents

Object-oriented---Chapter 11 IO object serialization

1.IO stream (IO stream)

2. open() method

3. Write method: write()

4. Object serialization


Object-oriented---Chapter 11 IO object serialization

1.IO stream (IO stream)

(1) Overview: Store files locally to form a persistent ability to read and write data

(2)I流

  • IO stream: input and output stream, which refers to a technology for reading data into memory and outputting it from memory
  • Role: Persist data to ensure that data is no longer lost

(3) Classification of streams :

  • Byte stream (b): Byte processing, all data can be manipulated, such as: audio, picture, executable file, byte stream When operating large data, it is not recommended to read it at one time
  • Character stream (t): Characters only operate UTF-8 character data, generally do not need to consider the problem of insufficient storage space (500w words-10MB)

2. open() method

(1) Process:

(2) buffer (buffer)

1) The buffer is a part of the memory, and a certain storage space is reserved in the memory, which is used to buffer input or output

The necessity of using the buffer: Since the I/O speed of the memory is much higher than the I/O speed of the peripherals, synchronous reading and writing will cause the memory to wait for a long time, wasting performance; it may cause data overflow or flooding

2) Buffer classification:

  • Full buffering : The actual transmission operation will only be performed when the standard I/O buffer is filled. The files on the hard disk use full buffering by default.
  • Line buffering : when the input and output encounter a newline character, it will be buffered
  • No buffering : the user does not provide buffering, and immediately reads and writes the data stream

(3) Format : f = open(filename,mode,encoding)

  • The return value of the open() method is a file object, which can be assigned to a variable (file handle)
  • filename : the file name, which is a string, including the path
  • encoding : Encoding format, generally utf-8
  • mode : the way to open the file

(4) b mode

1) Overview: Binary mode: generally used to read binary files such as pictures and videos

PS: The b mode is to read files in byte type, the returned byte object is not a string, the encoding format must be specified, and the input type must be guaranteed to be byte type

example:

s = 'this is a test'
b = bytes(s, encoding='utf-8')
f = open('test.txt', 'wb')
f.write(b)
f.close()
# 打开方式为wb,写入s串会报错

(5) + mode:

  • For w+ mode, the original data will be cleared before and after reading and writing, it is recommended not to use
  • For a+ mode, it will only be written at the end of the file forever, which has limitations and is not recommended
  • For the r+ mode, that is, the combination mode of reading and writing, cooperate with seek() and tell() methods to achieve more operations

(6) Operations on file objects:

  • read(size): Read data of a certain size, and then return it as a string or byte object, size is an optional parameter, used to specify the data stream for reading, size is ignored or negative, indicating that all contents of the file will be read fetch and return
f = open('test.txt', 'r')
str1 = f.read(7)
print(str1)
f.close()
# 若文件体积较大,可以分多次读取,使用read(512)方法一点点读取
  • readline(): read one or more lines from the file, change the line to \n, and return an empty string if the last line is read, it is used to read one line and process one line without turning back
f = open('test.txt', 'r')
str1 = f.readline()
print(str1)
f.close()
  • readlines(): Read all the lines of the file, line by line, into multiple lists, store them in the list in order, and return a list
f = open('test.txt', 'r')
str1 = f.readlines()
print(str1)
f.close()
# ['this is a test']

(7) Traversing files

  • You can actually use the file object as an iterator
f = open('test.txt', 'r')
for i in f:
    print(i,end=' ')
f.close()
# this is a test

Summary: There are several different methods of reading files. If the file capacity is small, it is more convenient to use read() to read it at one time. If the file size is not sure, you can use read(size) to test repeatedly. If it is a configuration file, you can use readlines() More convenient or for loop traversal

3. Write method: write()

  • Role: write string or byte data to a file
  • The multiple operations of write() are actually completed in memory, and will not be written to disk immediately. Only after close(), the operation is synchronized to disk
  • Format: file object.write('content')

(1) tell(): Returns the position of the file read and write pointer, the number of bytes counted from the beginning of the file

(2) seek(): Move several characters to the specified position, such as: seek(x,1) means to move x characters backward from the current position. seek(-x,2) moves forward from the end

f = open('test.txt', 'rb+')
f.write(b'123456789')
f.tell()

print(f.seek(2,2))
print(f.seek(3,1))
f.close()
# (x,数字),数字有0,1,2, 0表示从文件开头算起,1表示从文件读写指针的位置来时算起,2表示从文件的结尾算起,默认为0 

(3) close(): Close the file object. After processing a file, close the file and release the resource. If you try to read and write again, an exception will be thrown. If you forget to call close(), the result may be that the data will only Partially written, the rest of the data will be lost.

4. Object serialization

(1) What is the serialization of objects?

The process of transforming the abstract concept of objects such as containers into actual stored character or byte data.

(2) Causes:

  • Easy to store : The essence of serialization is to convert text information into a binary data stream, the data running in python. Such as: sequence, string tuple, etc. If you want to save it permanently for future use, you must serialize it.
  • Ease of transmission : When two processes communicate over a long distance, they can send various types of data to each other. No matter what type of data, they will be transmitted in binary sequence, and the receiver will deserialize it after receiving it, and convert it into a character that can be recognized. Sets are restored as objects.

(3) pickle module

  • Function: through the serialization operation of the pickle module, the running object information can be stored in a file and saved permanently, and the object can be recovered from the file through the pickle deserialization operation
  • Common methods: dump dumps load loads

# dumps 序列化为字节数据
import pickle
list1 = [10, 20, 30, 40, 50]
data1 = pickle.dumps(list1)
print(data1)
# b'\x80\x04\x95\x0f\x00\x00\x00\x00\x00\x00\x00]\x94(K\nK\x14K\x1eK(K2e.'
f = open('test2.txt', 'wb')
f.write(data1)
f.close()  
# loads 进行反序列化
import pickle
f = open('test2.txt', 'rb')
show = f.read()
show = pickle.loads(show)
print(show)
f.close()
# [10, 20, 30, 40, 50]

Example: Serialize and store strings in test3.txt, and deserialize the output after reading

# dump
import pickle

str1 = ['china', 'world', 'hello', '1234567']
pickle.dump(str1, open('test3.txt', 'wb'))
f = open('test3.txt', 'rb')
print(f.read())
f.close()

# load
str2 = pickle.load(open('test3.txt', 'rb'))
print(str2)

(4) json module

  • Function: the json module converts the object serial number into character data, the method is the same as above.

PS: json is generally used to process dictionary type data

import json
data1 = {'username': '杨勇', 'age': '17', 'number': '33'}
print(json.dumps(data1))
data2 = json.dumps(data1)
f = open('test4.txt', 'wt')
f.write(data2)
f.close()
# {"username": "\u6768\u52c7", "age": "17", "number": "33"}
# PS:注意文件打开方式为wt,字符形式

Guess you like

Origin blog.csdn.net/weixin_62443409/article/details/128262777