看板 Python 關於我們 聯絡資訊
各位晚安 小弟我想把一些 pickle 檔案轉成 json, 其中有一些 hash table,想把 key 拿掉,轉成 list。如 { "key001": { "key": 1, ... }, "key002": { "key": 2, ... }, ... } => [ { "key": 1, ... }, { "key": 2, ... }, ... ] 我原本以為設 JSONEncoder 就能解決 class Encoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, dict) and check_key_prop(obj): return list(obj.values()) return super().default(obj) 但完全沒有作用,只有 unserializable 的物件才會傳給 Encoder 不知道是不是我漏看了哪些參數 1. 原本官方設定就是這樣嗎?Encoder 只在不能 serialize 時才工作? 2. 還有其它比較建議的解決辦法嗎? -- (* ̄▽ ̄)/‧★*"`'*-.,_,.-*'`"*-.,_☆,.-*` http://i.imgur.com/oAd97.png
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.160.87.157 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1433678408.A.3E3.html
uranusjr: 1. 對 2. 沒有建議解法, 只能自己轉或重寫一個 06/07 23:17