java marshal 序列化,python使用marshal模块序列化实例

本文实例讲述了python使用marshal模块序列化的方法,分享给大家供大家参考。具体方法如下:

先来看看下面这段代码:

import marshal

data1 = ['abc',12,23,'jb51'] #几个测试数据

data2 = {1:'aaa',"b":'dad'}

data3 = (1,2,4)

output_file = open("a.txt",'wb')#把这些数据序列化到文件中,注:文件必须以二进制模式打开

marshal.dump(data1,output_file)

marshal.dump(data2,output_file)

marshal.dump(data3,output_file)

output_file.close()

input_file = open('a.txt','rb')#从文件中读取序列化的数据

#data1 = []

data1 = marshal.load(input_file)

data2 = marshal.load(input_file)

data3 = marshal.load(input_file)

print data1#给同志们打印出结果看看

print data2

print data3

outstring = marshal.dumps(data1)#marshal.dumps()返回是一个字节串,该字节串用于写入文件

open('out.txt','wb').write(outstring)

file_data = open('out.txt','rb').read()

real_data = marshal.loads(file_data)

print real_data

结果:

['abc', 12, 23, 'jb51']

{1: 'aaa', 'b': 'dad'}

(1, 2, 4)

['abc', 12, 23, 'jb51']

marshel模块的几个函数官方描述如下:

The module defines these functions:

marshal.dump(value, file[, version])

Write the value on the open file. The value must be a supported type. The file must be an open file object such as sys.stdout or returned by open() or os.popen(). It must be opened in binary mode ('wb' or 'w+b').

If the value has (or contains an object that has) an unsupported type, a ValueError exception is raised ― but garbage data will also be written to the file. The object will not be properly read back by load().

New in version 2.4: The version argument indicates the data format that dump should use (see below).

marshal.load(file)

Read one value from the open file and return it. If no valid value is read (e.g. because the data has a different Python version's incompatible marshal format), raise EOFError, ValueError or TypeError. The file must be an open file object opened in binary mode ('rb' or 'r+b').

Warning

If an object containing an unsupported type was marshalled with dump(), load() will substitute None for the unmarshallable type.

marshal.dumps(value[, version])

Return the string that would be written to a file by dump(value, file). The value must be a supported type. Raise a ValueError exception if value has (or contains an object that has) an unsupported type.

New in version 2.4: The version argument indicates the data format that dumps should use (see below).

marshal.loads(string)

Convert the string to a value. If no valid value is found, raise EOFError, ValueError or TypeError. Extra characters in the string are ignored.

In addition, the following constants are defined:

marshal.version

Indicates the format that the module uses.

marshal.version的用处

marshal不保证不同的python版本之间的兼容性,所以保留个版本信息的函数.

希望本文所述对大家Python程序设计的学习有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的XML序列是将Java对象转换为XML格式的过程。XML序列可以用于数据持久、数据传输等场景。Java提供了多种方式来实现XML序列,包括JAXB、XStream、Dom4j等。 JAXB是Java Architecture for XML Binding的缩写,是Java SE 6及以上版本中自带的一种XML数据绑定技术。JAXB通过注解或XML配置文件来描述Java类与XML之间的映射关系,从而实现Java对象到XML的序列和反序列。以下是一个使用JAXB进行XML序列的示例代码: ```java // 定义一个Java类 @XmlRootElement public class Person { private String name; private int age; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } // 序列Java对象到XML public static void serializeToXml(Person person, String xmlFilePath) throws JAXBException { JAXBContext context = JAXBContext.newInstance(Person.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(person, new File(xmlFilePath)); } // 反序列XML到Java对象 public static Person deserializeFromXml(String xmlFilePath) throws JAXBException { JAXBContext context = JAXBContext.newInstance(Person.class); Unmarshaller unmarshaller = context.createUnmarshaller(); return (Person) unmarshaller.unmarshal(new File(xmlFilePath)); } ``` 使用JAXB进行XML序列需要注意以下几点: 1. Java类需要使用@XmlRootElement注解标注为根元素。 2. Java类的属性需要使用@XmlAttribute或@XmlElement注解标注为XML属性或元素。 3. 序列时需要创建JAXBContext和Marshaller,反序列时需要创建JAXBContext和Unmarshaller。 4. 序列时可以设置Marshaller的属性,如是否格式输出。 除了JAXB外,XStream和Dom4j也是常用的XML序列工具。XStream是一款简单易用的XML序列框架,通过注解或代码配置来完成Java类与XML之间的映射关系。Dom4j是一款基于Java的XML解析器和生成器,可以快速方便地操作XML文档。无论使用哪种工具,XML序列都是Java开发中常用的技术之一。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值