gpt4 book ai didi

android - 在Android中读取python pickle数据流

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:38:22 26 4
gpt4 key购买 nike

我有这个包含 python pickle 数据流的文件。我必须在 Android 中读取此文件的内容。

例如,如果我想在 python 中读取这个数据流,我只需要使用下面的代码

queue = pickle.load(open('filename', 'rb'))

我想在 Android 中实现同样的事情,这样我就可以读取这个 pickle 流数据并将其存储在某种集合中。

我怎样才能做到这一点?

最佳答案

更新:这仅适用于 pickle 协议(protocol) 23

我认为 Unpickler来自 Pyrolite 的类(class)(麻省理工学院许可证)您可能特别感兴趣。它在技术上是 Java,但 Android 基本上是 Java。要取消 pickle ,您可以执行类似于以下操作的操作:

InputStream stream = new FileInputStream("filename");
Unpickler unpickler = new Unpickler();
Object data = unpickler.load(stream);
// And cast *data* to the appropriate type.

随着导入:

import java.io.FileInputStream;
import java.io.InputStream;
import net.razorvine.pickle.Unpickler;

这些是默认支持的对象:

PYTHON    ---->     JAVA
------ ----
None null
bool boolean
int int
long long or BigInteger (depending on size)
string String
unicode String
complex net.razorvine.pickle.objects.ComplexNumber
datetime.date java.util.Calendar
datetime.datetime java.util.Calendar
datetime.time java.util.Calendar
datetime.timedelta net.razorvine.pickle.objects.TimeDelta
float double (float isn't used)
array.array array of appropriate primitive type (char, int, short, long, float, double)
list java.util.List<Object>
tuple Object[]
set java.util.Set
dict java.util.Map
bytes byte[]
bytearray byte[]
decimal BigDecimal
custom class Map<String, Object> (dict with class attributes including its name in "__class__")

另请注意:

The unpickler simply returns an Object. Because Java is a statically typed language you will have to cast that to the appropriate type. Refer to this table to see what you can expect to receive.


更新:我使用各种 pickle 协议(protocol) (0-3) 运行测试,发现 01 失败,但是23 成功。

这是用于生成 pickle 数据的 python 代码:

import pickle

class Data(object):
def __init__(self):
self.x = 12

data = Data()

for p in [0, 1, 2]:
with open('data.{}'.format(p), 'wb') as fh:
pickle.dump(data, fh, protocol=p)

# Python 3 only.
with open('data.3', 'wb') as fh:
pickle.dump(data, fh, protocol=3)

以及解开它的 java 代码:

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.Map;
import net.razorvine.pickle.Unpickler;

public class Test {
public static void main(String[] args) throws IOException {
String filename = args[0];
InputStream inputStream = new FileInputStream(filename);
Unpickler unpickler = new Unpickler();
Map<String, Object> data = (Map<String, Object>)unpickler.load(inputStream);
}
}

当使用 data.0data.1 运行时,失败:

Exception in thread "main" net.razorvine.pickle.PickleException: expected zero arguments for construction of ClassDict (for copy_reg._reconstructor)
at net.razorvine.pickle.objects.ClassDictConstructor.construct(ClassDictConstructor.java:23)
at net.razorvine.pickle.Unpickler.load_reduce(Unpickler.java:617)
at net.razorvine.pickle.Unpickler.dispatch(Unpickler.java:170)
at net.razorvine.pickle.Unpickler.load(Unpickler.java:84)
at Test.main(Test.java:13)

data.2data.3运行时,成功。

关于android - 在Android中读取python pickle数据流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21794750/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com