Python Data Persistence 简明教程
Python Data Persistence - Object Serialization
Python 的 open() 内置函数返回的 Python 内置文件对象有一个重要的缺点。当使用“w”模式打开它时,write() 方法只接受字符串对象。
Python’s built-in file object returned by Python’s built-in open() function has one important shortcoming. When opened with 'w' mode, the write() method accepts only the string object.
这意味着,如果您有以任何非字符串形式表示的数据,无论是内置类(数字、字典、列表或元组)的对象还是其他用户定义的类,都不能直接写入文件。在写入之前,您需要将其转换为其字符串表示形式。
That means, if you have data represented in any non-string form, the object of either in built-in classes (numbers, dictionary, lists or tuples) or other user-defined classes, it cannot be written to file directly. Before writing, you need to convert it in its string representation.
numbers=[10,20,30,40]
file=open('numbers.txt','w')
file.write(str(numbers))
file.close()
对于二进制文件, write() 方法的参数必须是字节对象。例如,整数列表由 bytearray() 函数转换为字节,然后写入文件。
For a binary file, argument to write() method must be a byte object. For example, the list of integers is converted to bytes by bytearray() function and then written to file.
numbers=[10,20,30,40]
data=bytearray(numbers)
file.write(data)
file.close()
要从文件中读取相应数据类型的数据,需要进行反向转换。
To read back data from the file in the respective data type, reverse conversion needs to be done.
file=open('numbers.txt','rb')
data=file.read()
print (list(data))
这种将对象转换为字符串或字节格式(反之亦然)的手动转换非常麻烦和繁琐。有可能将 Python 对象的状态直接以字节流形式存储到文件或内存流中,并将其检索到其原始状态。这个过程称为序列化和反序列化。
This type of manual conversion, of an object to string or byte format (and vice versa) is very cumbersome and tedious. It is possible to store the state of a Python object in the form of byte stream directly to a file, or memory stream and retrieve to its original state. This process is called serialization and de-serialization.
Python 的内置库包含用于序列化和反序列化过程的各种模块。
Python’s built in library contains various modules for serialization and deserialization process.
Sr.No. |
Name & Description |
1 |
pickle Python specific serialization library |
2 |
marshal Library used internally for serialization |
3 |
shelve Pythonic object persistence |
4 |
dbm library offering interface to Unix database |
5 |
csv library for storage and retrieval of Python data to CSV format |
6 |
json Library for serialization to universal JSON format |