Python反序列化常用方法总结 - self
Python反序列化常用的方法有:
import pickle
with open('data.pkl', 'rb') as f: data = pickle.load(f)
2. 使用json模块进行反序列化:
```python
import json
with open('data.json', 'r') as f:
data = json.load(f)
import xml.etree.ElementTree as ET 拼多多帮助力入口
with open('data.xml', 'r') as f: tree = ET.parse(f) root = tree.getroot() data = [child.text for child in root]
4. 使用自定义的反序列化类:
```python
class MyClass:
def __init__(self, data):
self.data = data
def deserialize(self):
return self.data
my_obj = MyClass(json.dumps(data))