使用語句示例:
def Store(DataStructure, filename):
import pickle #使用pickle子產品
Fw =open( filename, ‘w’)
pickle.dump(DataStructure, Fw) #dump函數使用
Fw.close()
def Fetch( filename ):
import pickle #使用pickle子產品
Fr =open( filename)
return pickle.load(Fr)
參考連結:http://blog.csdn.net/uestcyao/article/details/7874817
(1):序列化的概念很簡單。記憶體裡面有一個資料結構,你希望将它儲存下來,重用,或者發送給其他人。你會怎麼做?嗯, 這取決于你想要怎麼儲存,怎麼重用,發送給誰。很多遊戲允許你在退出的時候儲存進度,然後你再次啟動的時候回到上次退出的地方。(實際上, 很多非遊戲程式也會這麼幹。) 在這個情況下, 一個捕獲了目前進度的資料結構需要在你退出的時候儲存到磁盤上,接着在你重新啟動的時候從磁盤上加載進來。這個資料隻會被建立它的程式使用,不會發送到網絡上,也不會被其它程式讀取。是以,互操作的問題被限制在保證新版本的程式能夠讀取以前版本的程式建立的資料。
在這種情況下,
pickle
子產品是理想的。它是Python标準庫的一部分, 是以它總是可用的。它很快; 它的大部分同Python解釋器本身一樣是用C寫的。 它可以存儲任意複雜的Python資料結構。
什麼東西能用
pickle
子產品存儲?
- 所有Python支援的 原生類型 : 布爾, 整數, 浮點數, 複數, 字元串,
(位元組串)對象, 位元組數組, 以及bytes
.None
- 由任何原生類型組成的清單,元組,字典和集合。
- 由任何原生類型組成的清單,元組,字典和集合組成的清單,元組,字典和集合(可以一直嵌套下去,直至Python支援的最大遞歸層數).
- 函數,類,和類的執行個體(帶警告)。
(2):源碼執行個體:
def storeSP(obj, filename):
import pickle as pk
fw = open(filename,'w')
protocol = fw
pk.dump(obj, protocol)
def fetchSP(filename):
import pickle as pk
fr =open(filename)
return pk.load(fr)
(3):序列化python對象被其他語言讀取:
pickle
子產品使用的資料格式是Python特定的。它沒有做任何相容其它程式設計語言的努力。如果跨語言相容是你的需求之一,你得去尋找其它的序列化格式。一個這樣的格式是json。 “json” 代表 “JavaScript Object Notation,” 但是不要讓名字糊弄你。 — json 是被設計為跨語言使用的。
Python 3 在标準庫中包含了一個
json
子產品。同
pickle
子產品類似,
json
子產品包含一些函數,可以序列化資料結構,儲存序列化後的資料至磁盤,從磁盤上讀取序列化後的資料,将資料反序列化成新的Pythone對象。但兩者也有一些很重要的差別。 首先, json資料格式是基于文本的, 不是二進制的。RFC 4627 定義了json格式以及怎樣将各種類型的資料編碼成文本。比如,一個布爾值要麼存儲為5個字元的字元串
'false'
,要麼存儲為4個字元的字元串
'true'
。 所有的json值都是大小寫敏感的。
第二,由于是文本格式, 存在空白(whitespaces)的問題。 json 允許在值之間有任意數目的空白(空格, 跳格, 回車,換行)。空白是“無關緊要的”,這意味着json編碼器可以按它們的喜好添加任意多或任意少的空白, 而json解碼器被要求忽略值之間的任意空白。這允許你“美觀的列印(pretty-print)” 你的 json 資料, 通過不同的縮進層次嵌套值,這樣你就可以在标準浏覽器或文本編輯器中閱讀它。Python 的
json
子產品有在編碼時執行美觀列印(pretty-printing)的選項。
第三, 字元編碼的問題是長期存在的。json 用純文字編碼資料, 但是你知道, “不存在純文字這種東西。” json必須以Unicode 編碼(UTF-32, UTF-16, 或者預設的, utf-8)方式存儲,RFC 4627的第3節 定義了如何區分使用的是哪種編碼。
将資料儲存至 json 檔案#
json 看起來非常像你在Javascript中手工定義的資料結構。這不是意外; 實際上你可以使用JavaScript 的
eval()
函數來“解碼” json序列化過的資料。(通常的對非信任輸入的警告也适用, 但關鍵點是json 是 合法的JavaScript。) 是以, 你可能已經熟悉json了。
>>> shell 1
>>> basic_entry = {} ①
>>> basic_entry['id'] = 256
>>> basic_entry['title'] = 'Dive into history, 2009 edition'
>>> basic_entry['tags'] = ('diveintopython', 'docbook', 'html')
>>> basic_entry['published'] = True
>>> basic_entry['comments_link'] = None
>>> import json
>>> with open('basic.json', mode='w', encoding='utf-8') as f: ②
... json.dump(basic_entry, f) ③
① | 我們将建立一個新的資料結構,而不是重用現存的entry資料結構。在這章的後面, 我們将會看見當我們試圖用json編碼更複雜的資料結構的時候會發生什麼。 |
② | json 是一個基于文本的格式, 這意味你可以以文本模式打開檔案,并給定一個字元編碼。用utf-8總是沒錯的。 |
③ | 同 子產品一樣, 子產品定義了 函數,它接受一個Python 資料結構和一個可寫的流對象。 函數将Python資料結構序列化并寫入到流對象中。在 語句内工作保證當我們完成的時候正确的關閉檔案。 |
那麼生成的json序列化資料是什麼樣的呢?
<div class="b" style="line-height:2.154; clear:left"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">[email protected]:~/diveintopython3/examples$ </samp><kbd style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154">cat basic.json</kbd>
<samp style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important">{"published": true, "tags": ["diveintopython", "docbook", "html"], "comments_link": null,
"id": 256, "title": "Dive into history, 2009 edition"}</samp></div>
這肯定比pickle 檔案更可讀。然而 json 的值之間可以包含任意數目的空把, 并且
json
子產品提供了一個友善的途徑來利用這一點生成更可讀的json檔案。
>>> shell 1
>>> with open('basic-pretty.json', mode='w', encoding='utf-8')as f:
... json.dump(basic_entry, f,indent=2)①
① | 如果你給 函數傳入indent參數, 它以檔案變大為代價使生成的json檔案更可讀。indent 參數是一個整數。0 意味着“每個值單獨一行。” 大于0的數字意味着“每個值單獨一行并且使用這個數目的空格來縮進嵌套的資料結構。” |
這是結果:
<div class="b" style="line-height:2.154; clear:left"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">[email protected]:~/diveintopython3/examples$ </samp><kbd style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154">cat basic-pretty.json</kbd>
<samp style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important">{
"published": true,
"tags": [
"diveintopython",
"docbook",
"html"
],
"comments_link": null,
"id": 256,
"title": "Dive into history, 2009 edition"
}</samp></div>
将Python資料類型映射到json#
由于json 不是Python特定的,對應到Python的資料類型的時候有很多不比對。有一些僅僅是名字不同,但是有兩個Python資料類型完全缺少。看看你能能把它們指出來:
筆記 | JSON | Python 3 |
---|---|---|
object | dictionary | |
array | list | |
string | string | |
integer | integer | |
real number | float | |
* | | |
* | | |
* | | |
* 所有的 json 值都是大小寫敏感的。 |
注意到什麼被遺漏了嗎?元組和 & 位元組串(bytes)! json 有數組類型,
json
子產品将其映射到Python的清單, 但是它沒有一個單獨的類型對應 “當機數組(frozen arrays)” (元組)。而且盡管 json 非常好的支援字元串,但是它沒有對
bytes
對象或位元組數組的支援。
⁂
序列化json不支援的資料類型#
即使json沒有内建的位元組流支援, 并不意味着你不能序列化
bytes
對象。
json
子產品提供了編解碼未知資料類型的擴充接口。(“未知”的意思是≴json沒有定義”。很顯然
json
子產品認識位元組數組, 但是它被json規範的限制束縛住了。) 如果你希望編碼位元組串或者其它json沒有原生支援的資料類型,你需要給這些類型提供定制的編碼和解碼器。
跳過該代碼清單
<div class="w" style="float:left">[<a target=_blank target="_blank" class="toggle" href="http://woodpecker.org.cn/diveintopython3/serializing.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; font-family:'Gill Sans','Gill Sans MT',Corbel,Helvetica,'Nimbus Sans L',sans-serif; line-height:2.154; word-spacing:0.1em; color:rgb(180,69,130); border-style:initial; border-color:initial">隐藏</a>] [<a target=_blank target="_blank" href="http://woodpecker.org.cn/diveintopython3/serializing.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; font-family:'Gill Sans','Gill Sans MT',Corbel,Helvetica,'Nimbus Sans L',sans-serif; line-height:2.154; word-spacing:0.1em; color:rgb(180,69,130); border-style:initial; border-color:initial">在新視窗中打開</a>]</div><div class="b" style="line-height:2.154; clear:left"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;">shell</span></kbd>
<samp class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important"><span class="lit" style="color:#06666;line-height:2.154;">1</span></samp>
<a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;">entry</span></kbd> <span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75">①</span></a>
<samp class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important"><span class="pun" style="color:#66660;line-height:2.154;">{</span><span class="str" style="color:#0880;line-height:2.154;">'comments_link'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">None</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'internal_id'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> b</span><span class="str" style="color:#0880;line-height:2.154;">'\xDE\xD5\xB4\xF8'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'title'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'Dive into history, 2009 edition'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'tags'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="str" style="color:#0880;line-height:2.154;">'diveintopython'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'docbook'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'html'</span><span class="pun" style="color:#66660;line-height:2.154;">),</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'article_link'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'http://diveintomark.org/archives/2009/03/27/dive-into-history-2009-edition'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'published_date'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> time</span><span class="pun" style="color:#66660;line-height:2.154;">.</span><span class="pln" style="color:#000000;line-height:2.154;">struct_time</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="pln" style="color:#000000;line-height:2.154;">tm_year</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="lit" style="color:#06666;line-height:2.154;">2009</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> tm_mon</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="lit" style="color:#06666;line-height:2.154;">3</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> tm_mday</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="lit" style="color:#06666;line-height:2.154;">27</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> tm_hour</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="lit" style="color:#06666;line-height:2.154;">22</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> tm_min</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="lit" style="color:#06666;line-height:2.154;">20</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> tm_sec</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="lit" style="color:#06666;line-height:2.154;">42</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> tm_wday</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="lit" style="color:#06666;line-height:2.154;">4</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> tm_yday</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="lit" style="color:#06666;line-height:2.154;">86</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> tm_isdst</span><span class="pun" style="color:#66660;line-height:2.154;">=-</span><span class="lit" style="color:#06666;line-height:2.154;">1</span><span class="pun" style="color:#66660;line-height:2.154;">),</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'published'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">True</span><span class="pun" style="color:#66660;line-height:2.154;">}</span></samp>
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="kwd" style="color:#0088;line-height:2.154;">import</span><span class="pln" style="color:#000000;line-height:2.154;"> json</span></kbd>
<a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="kwd" style="color:#0088;line-height:2.154;">with</span><span class="pln" style="color:#000000;line-height:2.154;"> open</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="str" style="color:#0880;line-height:2.154;">'entry.json'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'w'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> encoding</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="str" style="color:#0880;line-height:2.154;">'utf-8'</span><span class="pun" style="color:#66660;line-height:2.154;">)</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">as</span><span class="pln" style="color:#000000;line-height:2.154;"> f</span><span class="pun" style="color:#66660;line-height:2.154;">:</span></kbd> <span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75">②</span></a>
<a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">... </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;"> json</span><span class="pun" style="color:#66660;line-height:2.154;">.</span><span class="kwd" style="color:#0088;line-height:2.154;">dump</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="pln" style="color:#000000;line-height:2.154;">entry</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> f</span><span class="pun" style="color:#66660;line-height:2.154;">)</span></kbd> <span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75">③</span></a>
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">... </samp>
<samp class="traceback" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(136,0,0)">Traceback (most recent call last):
File "<stdin>", line 5, in <module>
File "C:\Python31\lib\json\__init__.py", line 178, in dump
for chunk in iterable:
File "C:\Python31\lib\json\encoder.py", line 408, in _iterencode
for chunk in _iterencode_dict(o, _current_indent_level):
File "C:\Python31\lib\json\encoder.py", line 382, in _iterencode_dict
for chunk in chunks:
File "C:\Python31\lib\json\encoder.py", line 416, in _iterencode
o = _default(o)
File "C:\Python31\lib\json\encoder.py", line 170, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: b'\xDE\xD5\xB4\xF8' is not JSON serializable</samp></div>
① | 好的, 是時間再看看entry 資料結構了。它包含了所有的東西: 布爾值, 值,字元串,字元串元組, 對象, 以及 結構體。 |
② | 我知道我已經說過了,但是這值得再重複一次:json 是一個基于文本的格式。總是應使用utf-8字元編碼以文本模式打開json檔案。 |
③ | 嗯,這可不好。發生什麼了? |
情況是這樣的:
json.dump()
函數試圖序列化
bytes
對象
b'\xDE\xD5\xB4\xF8'
,但是它失敗了,原因是json 不支援
bytes
對象。然而, 如果儲存位元組串對你來說很重要,你可以定義自己的“迷你序列化格式。”
跳過該代碼清單
<div class="w" style="float:left">[<a target=_blank target="_blank" class="toggle" href="http://woodpecker.org.cn/diveintopython3/serializing.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; font-family:'Gill Sans','Gill Sans MT',Corbel,Helvetica,'Nimbus Sans L',sans-serif; line-height:2.154; word-spacing:0.1em; color:rgb(180,69,130); border-style:initial; border-color:initial">隐藏</a>] [<a target=_blank target="_blank" href="http://woodpecker.org.cn/diveintopython3/serializing.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; font-family:'Gill Sans','Gill Sans MT',Corbel,Helvetica,'Nimbus Sans L',sans-serif; line-height:2.154; word-spacing:0.1em; color:rgb(180,69,130); border-style:initial; border-color:initial">在新視窗中打開</a>] [<a target=_blank target="_blank" href="http://woodpecker.org.cn/diveintopython3/examples/customserializer.py" target="_blank" rel="external nofollow" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; font-family:'Gill Sans','Gill Sans MT',Corbel,Helvetica,'Nimbus Sans L',sans-serif; line-height:2.154; word-spacing:0.1em; color:rgb(180,69,130); border-style:initial; border-color:initial">download <code style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; word-spacing:0px">customserializer.py</code></a>]
</div><div class="b" style="line-height:2.154; clear:left"><code style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important"><span class="pln" style="color:#000000;line-height:2.154;">
</span><a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><span class="kwd" style="color:#0088;line-height:2.154;">def</span><span class="pln" style="color:#000000;line-height:2.154;"> to_json</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="pln" style="color:#000000;line-height:2.154;">python_object</span><span class="pun" style="color:#66660;line-height:2.154;">):</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75"><span class="pun" style="line-height:1.75">①</span></span></a><span class="pln" style="color:#000000;line-height:2.154;">
</span><a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">if</span><span class="pln" style="color:#000000;line-height:2.154;"> isinstance</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="pln" style="color:#000000;line-height:2.154;">python_object</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> bytes</span><span class="pun" style="color:#66660;line-height:2.154;">):</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75"><span class="pun" style="line-height:1.75">②</span></span></a><span class="pln" style="color:#000000;line-height:2.154;">
</span><a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">return</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="pun" style="color:#66660;line-height:2.154;">{</span><span class="str" style="color:#0880;line-height:2.154;">'__class__'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'bytes'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'__value__'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> list</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="pln" style="color:#000000;line-height:2.154;">python_object</span><span class="pun" style="color:#66660;line-height:2.154;">)}</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75"><span class="pun" style="line-height:1.75">③</span></span></a><span class="pln" style="color:#000000;line-height:2.154;">
</span><a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">raise</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="typ" style="color:#66066;line-height:2.154;">TypeError</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="pln" style="color:#000000;line-height:2.154;">repr</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="pln" style="color:#000000;line-height:2.154;">python_object</span><span class="pun" style="color:#66660;line-height:2.154;">)</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="pun" style="color:#66660;line-height:2.154;">+</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">' is not JSON serializable'</span><span class="pun" style="color:#66660;line-height:2.154;">)</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75"><span class="pun" style="line-height:1.75">④</span></span></a></code></div>
① | 為了給一個json沒有原生支援的資料類型定義你自己的“迷你序列化格式”, 隻要定義一個接受一個Python對象為參數的函數。這個對象将會是 函數無法自己序列化的實際對象 — 這個例子裡是 對象 。 |
② | 你的自定義序列化函數應該檢查 函數傳給它的對象的類型。當你的函數隻序列化一個類型的時候這不是必須的,但是它使你的函數的覆寫的内容清楚明白,并且在你需要序列化更多類型的時候更容易擴充。 |
③ | 在這個例子裡面, 我将 對象轉換成字典。 鍵持有原始的資料類型(以字元串的形式, ), 而 鍵持有實際的資料。當然它不能是 對象; 大體的想法是将其轉換成某些可以被json序列化的東西! 對象就是一個範圍在0–255的整數的序列。 我們可以使用 函數将 對象轉換成整數清單。是以 變成 . (算一下! 這是對的! 16進制的位元組 是十進制的 222, 是 213, 以此類推。) |
④ | 這一行很重要。你序列化的資料結構可能包含json内建的可序列化類型和你的定制序列化器支援的類型之外的東西。在這種情況下,你的定制序列化器抛出一個 ,那樣 函數就可以知道你的定制序列化函數不認識該類型。 |
就這麼多;你不需要其它的東西。特别是, 這個定制序列化函數傳回Python字典,不是字元串。你不是自己做所有序列化到json的工作; 你僅僅在做轉換成被支援的類型那部分工作。
json.dump()
函數做剩下的事情。
跳過該代碼清單
<div class="w" style="float:left">[<a target=_blank target="_blank" class="toggle" href="http://woodpecker.org.cn/diveintopython3/serializing.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; font-family:'Gill Sans','Gill Sans MT',Corbel,Helvetica,'Nimbus Sans L',sans-serif; line-height:2.154; word-spacing:0.1em; color:rgb(180,69,130); border-style:initial; border-color:initial">隐藏</a>] [<a target=_blank target="_blank" href="http://woodpecker.org.cn/diveintopython3/serializing.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; font-family:'Gill Sans','Gill Sans MT',Corbel,Helvetica,'Nimbus Sans L',sans-serif; line-height:2.154; word-spacing:0.1em; color:rgb(180,69,130); border-style:initial; border-color:initial">在新視窗中打開</a>]</div><div class="b" style="line-height:2.154; clear:left"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;">shell</span></kbd>
<samp class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important"><span class="lit" style="color:#06666;line-height:2.154;">1</span></samp>
<a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="kwd" style="color:#0088;line-height:2.154;">import</span><span class="pln" style="color:#000000;line-height:2.154;"> customserializer</span></kbd> <span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75">①</span></a>
<a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="kwd" style="color:#0088;line-height:2.154;">with</span><span class="pln" style="color:#000000;line-height:2.154;"> open</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="str" style="color:#0880;line-height:2.154;">'entry.json'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'w'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> encoding</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="str" style="color:#0880;line-height:2.154;">'utf-8'</span><span class="pun" style="color:#66660;line-height:2.154;">)</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">as</span><span class="pln" style="color:#000000;line-height:2.154;"> f</span><span class="pun" style="color:#66660;line-height:2.154;">:</span></kbd> <span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75">②</span></a>
<a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">... </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;"> json</span><span class="pun" style="color:#66660;line-height:2.154;">.</span><span class="kwd" style="color:#0088;line-height:2.154;">dump</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="pln" style="color:#000000;line-height:2.154;">entry</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> f</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;font-weight:normal; line-height:2.154;">default</span><span class="pun" style="color:#66660;font-weight:normal; line-height:2.154;">=</span><span class="pln" style="font-weight:normal; line-height:2.154">customserializer</span><span class="pun" style="color:#66660;font-weight:normal; line-height:2.154;">.</span><span class="pln" style="font-weight:normal; line-height:2.154">to_json</span><span class="pun" style="color:#66660;line-height:2.154;">)</span></kbd> <span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75">③</span></a>
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">... </samp>
<samp class="traceback" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(136,0,0)">Traceback (most recent call last):
File "<stdin>", line 9, in <module>
json.dump(entry, f, default=customserializer.to_json)
File "C:\Python31\lib\json\__init__.py", line 178, in dump
for chunk in iterable:
File "C:\Python31\lib\json\encoder.py", line 408, in _iterencode
for chunk in _iterencode_dict(o, _current_indent_level):
File "C:\Python31\lib\json\encoder.py", line 382, in _iterencode_dict
for chunk in chunks:
File "C:\Python31\lib\json\encoder.py", line 416, in _iterencode
o = _default(o)
File "/Users/pilgrim/diveintopython3/examples/customserializer.py", line 12, in to_json
<a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> raise TypeError(repr(python_object) + ' is not JSON serializable') <span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;color:#222222;line-height:1.75;">④</span></a>
TypeError: time.struct_time(tm_year=2009, tm_mon=3, tm_mday=27, tm_hour=22, tm_min=20, tm_sec=42, tm_wday=4, tm_yday=86, tm_isdst=-1) is not JSON serializable</samp></div>
① | 子產品是你在前一個例子中定義 函數的地方。 |
② | 文本模式, utf-8 編碼, yadda yadda。(你很可能會忘記這一點! 我就忘記過好幾次! 事情一切正常直到它失敗的時刻, 而它的失敗很令人矚目。) |
③ | 這是重點: 為了将定制轉換函數鈎子嵌入 函數, 隻要将你的函數以default參數傳入 函數。(萬歲, Python裡一切皆對象!) |
④ | 好吧, 實際上還是不能工作。但是看一下異常。 函數不再抱怨無法序列化 對象了。現在它在抱怨另一個完全不同的對象: 對象。 |
盡管得到另一個不同的異常看起來不是什麼進步, 但它确實是個進步! 再調整一下就可以解決這個問題。
import time def to_json(python_object):
<div class="b" style="line-height:2.154; clear:left"><code style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important"></code><pre name="code" class="python">if isinstance(python_object, time.struct_time): ①
return {'__class__': 'time.asctime',
'__value__': time.asctime(python_object)} ②
if isinstance(python_object, bytes):
return {'__class__': 'bytes',
'__value__': list(python_object)
}
raise TypeError(repr(python_object) + ' is not JSON serializable')
① | 在現存的 函數裡面, 我們加入了Python 對象 ( 處理不了的那些) 是不是 的判斷。 |
② | 如果是的,我們做一些同處理 對象時類似的事情來轉換: 将 結構轉化成一個隻包含json可序列化值的字典。在這個例子裡, 最簡單的将日期時間轉換成json可序列化值的方法是使用 函數将其轉換成字元串。 函數将難看的 轉換成字元串 。 |
有了兩個定制的轉換, 整個entry 資料結構序列化到json應該沒有進一步的問題了。
跳過該代碼清單
<div class="w" style="float:left">[<a target=_blank target="_blank" class="toggle" href="http://woodpecker.org.cn/diveintopython3/serializing.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; font-family:'Gill Sans','Gill Sans MT',Corbel,Helvetica,'Nimbus Sans L',sans-serif; line-height:2.154; word-spacing:0.1em; color:rgb(180,69,130); border-style:initial; border-color:initial">隐藏</a>] [<a target=_blank target="_blank" href="http://woodpecker.org.cn/diveintopython3/serializing.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; font-family:'Gill Sans','Gill Sans MT',Corbel,Helvetica,'Nimbus Sans L',sans-serif; line-height:2.154; word-spacing:0.1em; color:rgb(180,69,130); border-style:initial; border-color:initial">在新視窗中打開</a>]</div><div class="b" style="line-height:2.154; clear:left"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;">shell</span></kbd>
<samp class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important"><span class="lit" style="color:#06666;line-height:2.154;">1</span></samp>
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="kwd" style="color:#0088;line-height:2.154;">with</span><span class="pln" style="color:#000000;line-height:2.154;"> open</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="str" style="color:#0880;line-height:2.154;">'entry.json'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'w'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> encoding</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="str" style="color:#0880;line-height:2.154;">'utf-8'</span><span class="pun" style="color:#66660;line-height:2.154;">)</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">as</span><span class="pln" style="color:#000000;line-height:2.154;"> f</span><span class="pun" style="color:#66660;line-height:2.154;">:</span></kbd>
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">... </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;"> json</span><span class="pun" style="color:#66660;line-height:2.154;">.</span><span class="kwd" style="color:#0088;line-height:2.154;">dump</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="pln" style="color:#000000;line-height:2.154;">entry</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> f</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">default</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="pln" style="color:#000000;line-height:2.154;">customserializer</span><span class="pun" style="color:#66660;line-height:2.154;">.</span><span class="pln" style="color:#000000;line-height:2.154;">to_json</span><span class="pun" style="color:#66660;line-height:2.154;">)</span></kbd>
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">... </samp></div>
跳過該代碼清單
<div class="w" style="float:left">[<a target=_blank target="_blank" class="toggle" href="http://woodpecker.org.cn/diveintopython3/serializing.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; font-family:'Gill Sans','Gill Sans MT',Corbel,Helvetica,'Nimbus Sans L',sans-serif; line-height:2.154; word-spacing:0.1em; color:rgb(180,69,130); border-style:initial; border-color:initial">隐藏</a>] [<a target=_blank target="_blank" href="http://woodpecker.org.cn/diveintopython3/serializing.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; font-family:'Gill Sans','Gill Sans MT',Corbel,Helvetica,'Nimbus Sans L',sans-serif; line-height:2.154; word-spacing:0.1em; color:rgb(180,69,130); border-style:initial; border-color:initial">在新視窗中打開</a>]</div><div class="b" style="line-height:2.154; clear:left"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">[email protected]:~/diveintopython3/examples$ </samp><kbd style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154">ls -l example.json</kbd>
<samp style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important">-rw-r--r-- 1 you you 391 Aug 3 13:34 entry.json</samp>
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">[email protected]:~/diveintopython3/examples$ </samp><kbd style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154">cat example.json</kbd>
<samp style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important">{"published_date": {"__class__": "time.asctime", "__value__": "Fri Mar 27 22:20:42 2009"},
"comments_link": null, "internal_id": {"__class__": "bytes", "__value__": [222, 213, 180, 248]},
"tags": ["diveintopython", "docbook", "html"], "title": "Dive into history, 2009 edition",
"article_link": "http://diveintomark.org/archives/2009/03/27/dive-into-history-2009-edition",
"published": true}</samp></div>
從json檔案加載資料#
類似
pickle
子產品,
json
子產品有一個
load()
函數接受一個流對象,從中讀取 json編碼過的資料, 并且建立該json資料結構的Python對象的鏡像。
跳過該代碼清單
<div class="w" style="float:left">[<a target=_blank target="_blank" class="toggle" href="http://woodpecker.org.cn/diveintopython3/serializing.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; font-family:'Gill Sans','Gill Sans MT',Corbel,Helvetica,'Nimbus Sans L',sans-serif; line-height:2.154; word-spacing:0.1em; color:rgb(180,69,130); border-style:initial; border-color:initial">隐藏</a>] [<a target=_blank target="_blank" href="http://woodpecker.org.cn/diveintopython3/serializing.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; font-family:'Gill Sans','Gill Sans MT',Corbel,Helvetica,'Nimbus Sans L',sans-serif; line-height:2.154; word-spacing:0.1em; color:rgb(180,69,130); border-style:initial; border-color:initial">在新視窗中打開</a>]</div><div class="b" style="line-height:2.154; clear:left"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;">shell</span></kbd>
<samp class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important"><span class="lit" style="color:#06666;line-height:2.154;">2</span></samp>
<a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="kwd" style="color:#0088;line-height:2.154;">del</span><span class="pln" style="color:#000000;line-height:2.154;"> entry</span></kbd> <span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75">①</span></a>
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;">entry</span></kbd>
<samp class="traceback" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(136,0,0)">Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'entry' is not defined</samp>
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="kwd" style="color:#0088;line-height:2.154;">import</span><span class="pln" style="color:#000000;line-height:2.154;"> json</span></kbd>
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="kwd" style="color:#0088;line-height:2.154;">with</span><span class="pln" style="color:#000000;line-height:2.154;"> open</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="str" style="color:#0880;line-height:2.154;">'entry.json'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'r'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> encoding</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="str" style="color:#0880;line-height:2.154;">'utf-8'</span><span class="pun" style="color:#66660;line-height:2.154;">)</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">as</span><span class="pln" style="color:#000000;line-height:2.154;"> f</span><span class="pun" style="color:#66660;line-height:2.154;">:</span></kbd>
<a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">... </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;"> entry </span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="pln" style="color:#000000;line-height:2.154;"> json</span><span class="pun" style="color:#66660;line-height:2.154;">.</span><span class="pln" style="color:#000000;line-height:2.154;">load</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="pln" style="color:#000000;line-height:2.154;">f</span><span class="pun" style="color:#66660;line-height:2.154;">)</span></kbd> <span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75">②</span></a>
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">... </samp>
<a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;">entry</span></kbd> <span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75">③</span></a>
<samp class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important"><span class="pun" style="color:#66660;line-height:2.154;">{</span><span class="str" style="color:#0880;line-height:2.154;">'comments_link'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">None</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'internal_id'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="pun" style="color:#66660;line-height:2.154;">{</span><span class="str" style="color:#0880;line-height:2.154;">'__class__'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'bytes'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'__value__'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="pun" style="color:#66660;line-height:2.154;">[</span><span class="lit" style="color:#06666;line-height:2.154;">222</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="lit" style="color:#06666;line-height:2.154;">213</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="lit" style="color:#06666;line-height:2.154;">180</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="lit" style="color:#06666;line-height:2.154;">248</span><span class="pun" style="color:#66660;line-height:2.154;">]},</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'title'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'Dive into history, 2009 edition'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'tags'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="pun" style="color:#66660;line-height:2.154;">[</span><span class="str" style="color:#0880;line-height:2.154;">'diveintopython'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'docbook'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'html'</span><span class="pun" style="color:#66660;line-height:2.154;">],</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'article_link'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'http://diveintomark.org/archives/2009/03/27/dive-into-history-2009-edition'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'published_date'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="pun" style="color:#66660;line-height:2.154;">{</span><span class="str" style="color:#0880;line-height:2.154;">'__class__'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'time.asctime'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'__value__'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'Fri Mar 27 22:20:42 2009'</span><span class="pun" style="color:#66660;line-height:2.154;">},</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'published'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">True</span><span class="pun" style="color:#66660;line-height:2.154;">}</span></samp></div>
① | 為了示範目的,切換到Python Shell #2 并且删除在這一章前面使用 子產品建立的entry資料結構。 |
② | 最簡單的情況下, 函數同 函數的結果一模一樣。你傳入一個流對象,它傳回一個新的Python對象。 |
③ | 有好消息也有壞消息。好消息先來: 函數成功的讀取了你在Python Shell #1中建立的 檔案并且生成了一個包含那些資料的新的Python對象。接着是壞消息: 它沒有重建原始的 entry 資料結構。 和 這兩個值被重建為字典 — 具體來說, 你在 轉換函數中使用json相容的值建立的字典。 |
json.load()
并不知道你可能傳給
json.dump()
的任何轉換函數的任何資訊。你需要的是
to_json()
函數的逆函數 — 一個接受定制轉換出的json 對象并将其轉換回原始的Python資料類型。
跳過該代碼清單
<div class="w" style="float:left">[<a target=_blank target="_blank" class="toggle" href="http://woodpecker.org.cn/diveintopython3/serializing.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; font-family:'Gill Sans','Gill Sans MT',Corbel,Helvetica,'Nimbus Sans L',sans-serif; line-height:2.154; word-spacing:0.1em; color:rgb(180,69,130); border-style:initial; border-color:initial">隐藏</a>] [<a target=_blank target="_blank" href="http://woodpecker.org.cn/diveintopython3/serializing.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; font-family:'Gill Sans','Gill Sans MT',Corbel,Helvetica,'Nimbus Sans L',sans-serif; line-height:2.154; word-spacing:0.1em; color:rgb(180,69,130); border-style:initial; border-color:initial">在新視窗中打開</a>]</div><div class="b" style="line-height:2.154; clear:left"><code style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important"><span class="com" style="color:#8800;line-height:2.154;"># add this to customserializer.py</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><span class="kwd" style="color:#0088;line-height:2.154;">def</span><span class="pln" style="color:#000000;line-height:2.154;"> from_json</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="pln" style="color:#000000;line-height:2.154;">json_object</span><span class="pun" style="color:#66660;line-height:2.154;">):</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75"><span class="pun" style="line-height:1.75">①</span></span></a><span class="pln" style="color:#000000;line-height:2.154;">
</span><a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">if</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'__class__'</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">in</span><span class="pln" style="color:#000000;line-height:2.154;"> json_object</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75"><span class="pun" style="line-height:1.75">②</span></span></a><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="kwd" style="color:#0088;line-height:2.154;">if</span><span class="pln" style="color:#000000;line-height:2.154;"> json_object</span><span class="pun" style="color:#66660;line-height:2.154;">[</span><span class="str" style="color:#0880;line-height:2.154;">'__class__'</span><span class="pun" style="color:#66660;line-height:2.154;">]</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="pun" style="color:#66660;line-height:2.154;">==</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'time.asctime'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">return</span><span class="pln" style="color:#000000;line-height:2.154;"> time</span><span class="pun" style="color:#66660;line-height:2.154;">.</span><span class="pln" style="color:#000000;line-height:2.154;">strptime</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="pln" style="color:#000000;line-height:2.154;">json_object</span><span class="pun" style="color:#66660;line-height:2.154;">[</span><span class="str" style="color:#0880;line-height:2.154;">'__value__'</span><span class="pun" style="color:#66660;line-height:2.154;">])</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75"><span class="pun" style="line-height:1.75">③</span></span></a><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="kwd" style="color:#0088;line-height:2.154;">if</span><span class="pln" style="color:#000000;line-height:2.154;"> json_object</span><span class="pun" style="color:#66660;line-height:2.154;">[</span><span class="str" style="color:#0880;line-height:2.154;">'__class__'</span><span class="pun" style="color:#66660;line-height:2.154;">]</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="pun" style="color:#66660;line-height:2.154;">==</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'bytes'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">return</span><span class="pln" style="color:#000000;line-height:2.154;"> bytes</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="pln" style="color:#000000;line-height:2.154;">json_object</span><span class="pun" style="color:#66660;line-height:2.154;">[</span><span class="str" style="color:#0880;line-height:2.154;">'__value__'</span><span class="pun" style="color:#66660;line-height:2.154;">])</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75"><span class="pun" style="line-height:1.75">④</span></span></a><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="kwd" style="color:#0088;line-height:2.154;">return</span><span class="pln" style="color:#000000;line-height:2.154;"> json_object</span></code></div>
① | 這函數也同樣接受一個參數傳回一個值。但是參數不是字元串,而是一個Python對象 — 反序列化一個json編碼的字元串為Python的結果。 |
② | 你隻需要檢查這個對象是否包含 函數建立的 鍵。如果是的, 鍵對應的值将告訴你如何将值解碼成原來的Python資料類型。 |
③ | 為了解碼由 函數傳回的字元串,你要使用 函數。這個函數接受一個格式化過的時間字元串(格式可以自定義,但預設值同 函數的預設值相同) 并且傳回 . |
④ | 為了将整數清單轉換回 對象, 你可以使用 函數。 |
就是這樣;
to_json()
函數處理了兩種資料類型,現在這兩個資料類型也在
from_json()
函數裡面處理了。下面是結果:
跳過該代碼清單
<div class="w" style="float:left">[<a target=_blank target="_blank" class="toggle" href="http://woodpecker.org.cn/diveintopython3/serializing.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; font-family:'Gill Sans','Gill Sans MT',Corbel,Helvetica,'Nimbus Sans L',sans-serif; line-height:2.154; word-spacing:0.1em; color:rgb(180,69,130); border-style:initial; border-color:initial">隐藏</a>] [<a target=_blank target="_blank" href="http://woodpecker.org.cn/diveintopython3/serializing.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; font-family:'Gill Sans','Gill Sans MT',Corbel,Helvetica,'Nimbus Sans L',sans-serif; line-height:2.154; word-spacing:0.1em; color:rgb(180,69,130); border-style:initial; border-color:initial">在新視窗中打開</a>]</div><div class="b" style="line-height:2.154; clear:left"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;">shell</span></kbd>
<samp class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important"><span class="lit" style="color:#06666;line-height:2.154;">2</span></samp>
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="kwd" style="color:#0088;line-height:2.154;">import</span><span class="pln" style="color:#000000;line-height:2.154;"> customserializer</span></kbd>
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="kwd" style="color:#0088;line-height:2.154;">with</span><span class="pln" style="color:#000000;line-height:2.154;"> open</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="str" style="color:#0880;line-height:2.154;">'entry.json'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'r'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> encoding</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="str" style="color:#0880;line-height:2.154;">'utf-8'</span><span class="pun" style="color:#66660;line-height:2.154;">)</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">as</span><span class="pln" style="color:#000000;line-height:2.154;"> f</span><span class="pun" style="color:#66660;line-height:2.154;">:</span></kbd>
<a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">... </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;"> entry </span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="pln" style="color:#000000;line-height:2.154;"> json</span><span class="pun" style="color:#66660;line-height:2.154;">.</span><span class="pln" style="color:#000000;line-height:2.154;">load</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="pln" style="color:#000000;line-height:2.154;">f</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> object_hook</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="pln" style="color:#000000;line-height:2.154;">customserializer</span><span class="pun" style="color:#66660;line-height:2.154;">.</span><span class="pln" style="color:#000000;line-height:2.154;">from_json</span><span class="pun" style="color:#66660;line-height:2.154;">)</span></kbd> <span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75">①</span></a>
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">... </samp>
<a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;">entry</span></kbd> <span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75">②</span></a>
<samp class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important"><span class="pun" style="color:#66660;line-height:2.154;">{</span><span class="str" style="color:#0880;line-height:2.154;">'comments_link'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">None</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'internal_id'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> b</span><span class="str" style="color:#0880;line-height:2.154;">'\xDE\xD5\xB4\xF8'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'title'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'Dive into history, 2009 edition'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'tags'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="pun" style="color:#66660;line-height:2.154;">[</span><span class="str" style="color:#0880;line-height:2.154;">'diveintopython'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'docbook'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'html'</span><span class="pun" style="color:#66660;line-height:2.154;">],</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'article_link'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'http://diveintomark.org/archives/2009/03/27/dive-into-history-2009-edition'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'published_date'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> time</span><span class="pun" style="color:#66660;line-height:2.154;">.</span><span class="pln" style="color:#000000;line-height:2.154;">struct_time</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="pln" style="color:#000000;line-height:2.154;">tm_year</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="lit" style="color:#06666;line-height:2.154;">2009</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> tm_mon</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="lit" style="color:#06666;line-height:2.154;">3</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> tm_mday</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="lit" style="color:#06666;line-height:2.154;">27</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> tm_hour</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="lit" style="color:#06666;line-height:2.154;">22</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> tm_min</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="lit" style="color:#06666;line-height:2.154;">20</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> tm_sec</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="lit" style="color:#06666;line-height:2.154;">42</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> tm_wday</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="lit" style="color:#06666;line-height:2.154;">4</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> tm_yday</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="lit" style="color:#06666;line-height:2.154;">86</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> tm_isdst</span><span class="pun" style="color:#66660;line-height:2.154;">=-</span><span class="lit" style="color:#06666;line-height:2.154;">1</span><span class="pun" style="color:#66660;line-height:2.154;">),</span><span class="pln" style="color:#000000;line-height:2.154;">
</span><span class="str" style="color:#0880;line-height:2.154;">'published'</span><span class="pun" style="color:#66660;line-height:2.154;">:</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">True</span><span class="pun" style="color:#66660;line-height:2.154;">}</span></samp></div>
① | 為了将 函數嵌入到反序列化過程中,把它作為object_hook 參數傳入到 函數中。接受函數作為參數的函數; 真友善! |
② | entry 資料結構現在有一個值為 對象的 鍵。它也包含一個 鍵,其值為 對象。 |
然而,還有最後一個缺陷。
跳過該代碼清
<div class="b" style="line-height:2.154; clear:left">
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="kwd" style="color:#0088;line-height:2.154;">import</span><span class="pln" style="color:#000000;line-height:2.154;"> customserializer</span></kbd>
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="kwd" style="color:#0088;line-height:2.154;">with</span><span class="pln" style="color:#000000;line-height:2.154;"> open</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="str" style="color:#0880;line-height:2.154;">'entry.json'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'r'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> encoding</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="str" style="color:#0880;line-height:2.154;">'utf-8'</span><span class="pun" style="color:#66660;line-height:2.154;">)</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="kwd" style="color:#0088;line-height:2.154;">as</span><span class="pln" style="color:#000000;line-height:2.154;"> f</span><span class="pun" style="color:#66660;line-height:2.154;">:</span></kbd>
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">... </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;"> entry2 </span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="pln" style="color:#000000;line-height:2.154;"> json</span><span class="pun" style="color:#66660;line-height:2.154;">.</span><span class="pln" style="color:#000000;line-height:2.154;">load</span><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="pln" style="color:#000000;line-height:2.154;">f</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> object_hook</span><span class="pun" style="color:#66660;line-height:2.154;">=</span><span class="pln" style="color:#000000;line-height:2.154;">customserializer</span><span class="pun" style="color:#66660;line-height:2.154;">.</span><span class="pln" style="color:#000000;line-height:2.154;">from_json</span><span class="pun" style="color:#66660;line-height:2.154;">)</span></kbd>
<samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">... </samp>
<a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;">entry2 </span><span class="pun" style="color:#66660;line-height:2.154;">==</span><span class="pln" style="color:#000000;line-height:2.154;"> entry</span></kbd> <span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75">①</span></a>
<samp class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important"><span class="kwd" style="color:#0088;line-height:2.154;">False</span></samp>
<a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;">entry</span><span class="pun" style="color:#66660;line-height:2.154;">[</span><span class="str" style="color:#0880;line-height:2.154;">'tags'</span><span class="pun" style="color:#66660;line-height:2.154;">]</span></kbd> <span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75">②</span></a>
<samp class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important"><span class="pun" style="color:#66660;line-height:2.154;">(</span><span class="str" style="color:#0880;line-height:2.154;">'diveintopython'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'docbook'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'html'</span><span class="pun" style="color:#66660;line-height:2.154;">)</span></samp>
<a target=_blank target="_blank" style="border-bottom-width:0px; padding-top:0.4375em; padding-right:0px; padding-bottom:0.4375em; padding-left:0px; border-top-width:0px; border-right-width:0px; border-left-width:0px; border-style:initial; border-color:initial"><samp class="p" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important; color:rgb(102,102,119)">>>> </samp><kbd class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:2.154"><span class="pln" style="color:#000000;line-height:2.154;">entry2</span><span class="pun" style="color:#66660;line-height:2.154;">[</span><span class="str" style="color:#0880;line-height:2.154;">'tags'</span><span class="pun" style="color:#66660;line-height:2.154;">]</span></kbd> <span class="u" style="font-family:'Arial Unicode MS','DejaVu Sans',FreeSerif,OpenSymbol,sans-serif;font-size:14px;line-height:1.75">③</span></a>
<samp class="pp" style="font-family:Consolas,'Andale Mono',Monaco,'Liberation Mono','Bitstream Vera Sans Mono','DejaVu Sans Mono',monospace; line-height:inherit!important"><span class="pun" style="color:#66660;line-height:2.154;">[</span><span class="str" style="color:#0880;line-height:2.154;">'diveintopython'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'docbook'</span><span class="pun" style="color:#66660;line-height:2.154;">,</span><span class="pln" style="color:#000000;line-height:2.154;"> </span><span class="str" style="color:#0880;line-height:2.154;">'html'</span><span class="pun" style="color:#66660;line-height:2.154;">]</span></samp></div>
① | 即使在序列化過程中加入了 鈎子函數, 也在反序列化過程中加入 鈎子函數, 我們仍然沒有重新建立原始資料結構的完美複制品。為什麼沒有? |
② | 在原始的entry 資料結構中, 鍵的值為一個三個字元串組成的元組。 |
③ | 但是重制建立的entry2 資料結構中, 鍵的值是一個三個字元串組成的清單。json 并不區分元組和清單;它隻有一個類似清單的資料類型,數組,并且 子產品在序列化過程中會安靜的将元組和清單兩個都轉換成json 數組。大多數情況下,你可以忽略元組和清單的差別,但是在使用 子產品時應記得有這麼一回使。 |
擴充閱讀:
很多關于
子產品的文章提到了
pickle
。在Python 2中,
cPickle
子產品有兩個實作, 一個由純Python寫的而另一個用C寫的(但仍然可以在Python中調用)。在Python 3中, 這兩個子產品已經合并, 是以你總是簡單的
pickle
就可以。你可能會發現這些文章很有用,但是你應該忽略已過時的關于的
import pickle
的資訊.
cPickle
使用
pickle
子產品打包:
-
modulepickle
-
andpickle
— Python object serializationcPickle
- Using
pickle
- Python persistence management
使用json 和
json
子產品:
-
— JavaScript Object Notation Serializerjson
- JSON encoding and ecoding with custom objects in Python
擴充打包:
- Pickling class instances
- Persistence of external objects
- Handling stateful objects