# pickle持久化

**1.保存bytes形式**dumps()

把任意对象序列化成一个bytes，然后，就可以把这个bytes写入文件，或者通过网络传输。&#x20;

```python
import pickle

d = dict(name='Bob', age=20, score=88)
bts=pickle.dumps(d)
print type(bts)# 输出：<type 'str'>

#加载方式，读取bytes
bj2 = pickle.loads(bts) 
print type(obj2)# 输出：<type 'tuple'>   
print obj2# 输出：d {'age': 20, 'score': 88, 'name': 'Bob'}
```

**2.文件形式，dump，load**

直接把对象序列化后写入一个file-like Object：

```python
f = open('dump.txt', 'wb') 
pickle.dump(d, f) 
f.close() 

f = open('dump.txt', 'rb')
d = pickle.load(f)
f.close()
#d {'age': 20, 'score': 88, 'name': 'Bob'}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/python/pickle-chi-jiu-hua.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
