在Python中marshal对象序列化的相关知识
有时候,要把内存中的一个对象持久化保存到磁盘上,或者序列化成二进制流通过网络发送到远程主机上。Python中有很多模块提供了序列化与反序列化的功能,如:marshal, pickle, cPickle等等。今天就讲讲marshal模块。
- 注意: marshal并不是一个通用的模块,在某些时候它是一个不被推荐使用的模块,因为使用marshal序列化的二进制数据格式还没有文档化,在不同版本的Python中,marshal的实现可能不一样。也就是说,用python2.5序列为一个对象,用python2.6的程序反序列化所得到的对象,可能与原来的对象是不一样的。但这个模块存在的意义,正如Python手册中所说:The marshal module exists mainly to support reading and writing the “pseudo-compiled” code for Python modules of .pyc files.
下面是marshal模块中定义的一些与序列化/反序列化有关的函数:
marshal.dump(value, file[, version])
将值写入到一个打开的输出流里。参数value表示待序列化的值。file表示打开的输出流。如:以”wb”模式打开的文件,sys.stdout或者os.popen。对于一些不支持序列类的类型,dump方法将抛出ValueError异常。要特别说明一下,并不是所有类型的对象都可以使用marshal模块来序列化/反序列化的。在python2.6中,支持的类型包括:None, integers, long integers, floating point numbers, strings, Unicode objects, tuple, list, set, dict, 和 code objects。对于tuple, list, set, dict等集合对象,其中的元素必须也是上述类型之一。
marshal.load(file)
执行与marshal.dump相反的操作,将二进制数据反序列为Python对象。下面是一个例子,演示这两个方法的使用:
# coding=gbk import marshal , sys , os lst = [ 1 , ( 2 , " string " ) , { " key " : " Value " } ] # 序列化到文件中 fle = open ( os . path . join ( os . getcwd ( ) , ' fle . txt ' ) , ' wb ' ) marshal . dump ( lst , fle ) fle . close ( ) # 反序列化 fle1 = open ( os . path . join ( os . getcwd ( ) , ' fle . txt ' ) , ' rb ' ) lst1 = marshal . load ( fle1 ) fle1 . close ( ) # 打印结果 print lst print lst1 # ---- 结果 ---- # [1, (2, 'string'), {'key': 'Value'}] # [1, (2, 'string'), {'key': 'Value'}] marshal.dumps(value[, version)
该方法与上面讲的marshal.dump()功能类似,只是它返回的是序列化之后的二进制流,而不是将这些数据直接写入到文件中。
marsahl.load(string)
将二进制流反序列化为对象。下面的一段代码,演示这两个方法的使用:
import marshal , sys , os lst = [ 1 , ( 2 , " string " ) , { " key " : " Value " } ] byt1 = marshal . dumps ( lst ) lst1 = marshal . loads ( byt1 ) # 打印结果 print lst print lst1 # —- 结果 —- # [1, (2, 'string'), {'key': 'Value'}] # [1, (2, 'string'), {'key': 'Value'}]
更多关于marshal的内容,请参考Python手册。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

The key to running Jupyter Notebook in VS Code is to ensure that the Python environment is properly configured, understand that the code execution order is consistent with the cell order, and be aware of large files or external libraries that may affect performance. The code completion and debugging functions provided by VS Code can greatly improve coding efficiency and reduce errors.
