简体   繁体   English

你如何在 Java 中解开一个 python 对象

[英]How do you unpickle a python object in Java

I am trying unpickle an xgboost model within Java to load it.我正在尝试在 Java 中解开一个 xgboost 模型来加载它。 Is there any way to do this.有没有办法做到这一点。 It's difficult for me to run purely in python.我很难完全在 python 中运行。

I've been thinking of programmatically writing and executing a python script but is there a cleaner way?我一直在考虑以编程方式编写和执行 python 脚本,但是有更简洁的方法吗? Solutions like Jython are outdated because I am using Python 3.8.像 Jython 这样的解决方案已经过时了,因为我使用的是 Python 3.8。

See the awesome Pickle library.查看很棒的Pickle库。

The usage is very simple:用法很简单:

Object pyObject;
try(InputStream is = new FileInputStream("dump.pkl")){
  net.razorvine.pickle.Unpickler unpickler = new net.razorvine.pickle.Unpickler();
  pyObject = unpickler.load(is);
}

If the pickle file contains a Python object, then the loaded pyObject is a Java map that contains key-value attribute mappings.如果 pickle 文件包含 Python 对象,则加载的pyObject是包含键值属性映射的 Java 映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM