在Python和IronPython之间传递pickle

MPJ*_*MPJ 9 python clr ironpython pickle importerror

我在使用IronPython中转储的Python加载pickle时遇到了困难.

当我在IronPython中挑选像"[1,2,3]"这样的基本内容时,pickle在Python中加载得很好.但是,当我在IronPython中从下面的数据库查询中挑选结果时,在尝试加载Python中的pickle时,我收到错误"没有名为clr的模块".

可能出了什么问题?(有没有更好的方法在Python和IronPython之间共享数据?)

def GetData(id):
    TheConnection = SqlClient.SqlConnection("server=MyServer;database=MyDB;Trusted_Connection=yes")
    TheConnection.Open()
    sqlcommand = "MyStoredProcedure#GetMyData '%s'" %id

    MyAction = SqlClient.SqlCommand(sqlcommand, TheConnection)
    MyReader = MyAction.ExecuteReader()

    results = list()

    while MyReader.Read():
        row = {
               'Type':'LolCat',
               'Date':datetime.datetime(MyReader[1]),
               'Location':str(MyReader[3]),
               'Weight':float(MyReader[6])/float(MyReader[7]),
               }
        results.append(row)

    TheConnection.Close()
    MyReader.Close()

    return results



results1 = GetData(1234)
results2 = GetData(2345)

...
picklefile= open("testpickle","w")
cPickle.dump((results1,results2),picklefile)

...
picklefile = open("testpickle","r")
p = cPickle.load(file)  # this is the line that gives the error "ImportError: No module named clr"
Run Code Online (Sandbox Code Playgroud)

the*_*aul 4

Pickling 确实是在 python 之间共享数据的一种很好的通用方法(当你可以信任数据并知道它没有被篡改时)。但它只适用于简单的内置类型。当您有特殊类型的对象(例如无论结果是什么GetData())时,pickling 将嵌入该类的完全限定名称,并希望另一端的 python 知道如何找到该类并重新实例化它。不用说,如果你不小心,这可能会变得非常混乱。

尝试将results1,results2值转换为简单类型,例如(可能是嵌套的)元组、字典、列表等。