解开python dict对象

时间:2019-06-04 08:03:24

标签: python go pickle golang-migrate

我有一些存储在Redis中的数据,这些数据由python应用程序腌制后保存。所以基本上这是一个腌制的python dict对象。我想在golang中获取此存储的数据。 我找到了一个图书馆:https://godoc.org/github.com/hydrogen18/stalecucumber

但是,当我尝试一些基本示例来消除其出现错误时,例如:

string:Pickle Machine failed on opcode:0x62. Stack size:0. Memo size:0. Cause:Stack is too small to perform requested operation
string:Pickle Machine failed on opcode:0x5c. Stack size:0. Memo size:0. Cause:Opcode is invalid

有关更多说明,请参见下面的示例。

  • Data = pickle.dumps({"apple":1,"banana":2,"cat":"hello","Dog":42.0}) #python腌制
  • 腌制数据= b'\x80\x03}q\x00(X\x05\x00\x00\x00appleq\x01K\x01X\x06\x00\x00\x00bananaq\x02K\x02X\x03\x00\x00\x00catq\x03X\x05\x00\x00\x00helloq\x04X\x03\x00\x00\x00Dogq\x05G@E\x00\x00\x00\x00\x00\x00u.'

我尝试在Go中解锁:

package main

import (
    "fmt"
    "stalecucumber"
    "io"
    "os"
)    

func main() {
    var pickledDict io.Reader
    var err error
    pickledDict, err = os.Open("file.txt")
    fmt.Println(pickledDict)
    mystruct := struct{
        Apple  int
        Banana uint
        Cat    string
        Dog    float32
    }{}

    err = stalecucumber.UnpackInto(&mystruct).From(stalecucumber.Unpickle(pickledDict))
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(mystruct)
}

获取错误:

Pickle Machine failed on opcode:0x62. Stack size:0. Memo size:0. Cause:Stack is too small to perform requested operation
{0 0  0}

0 个答案:

没有答案