阅读Android的蟒蛇泡菜数据流 [英] Read python pickle data stream in Android

查看:302
本文介绍了阅读Android的蟒蛇泡菜数据流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个文件,其中包含蟒蛇泡菜数据流。我已经阅读本文件中的Andr​​oid内容。

I have this file which contains python pickle data stream. I've to read contents of this file in Android.

例如,如果我想读蟒蛇这一数据流,我只是使用下面的code

For example, if I wanted to read this data stream in python, I'd just use the following code

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

我要实现同样的事情在Android的,这样我可以读这咸菜流数据,并将其存储在某些类型的集合。

I want to achieve same thing in Android such that I can read this pickle stream data and store it in some kind of collection.

我怎样才能做到这一点?

How can I achieve this?

先谢谢了。

-Rajesh

推荐答案

更​​新:这只适用于咸菜协议 2 3

UPDATE: This only works for pickle protocols 2 and 3.

我觉得 Unpickler会 地幔岩(MIT许可)类可能是您特别感兴趣的。这在技术上是Java,但是Android是基本的Java。要unpickle你会做类似下面的内容:

I think the Unpickler class from Pyrolite (MIT license) may be of particular interest to you. It is technically Java, but Android is basically Java. To unpickle you would do something similar to the following:

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;

这些都是默认支持的对象:

These are the objects supported by default:

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__")

另外请注意:

该Unpickler会简单的返回一个对象。因为Java是一种静态类型
  语言,你将不得不强制转换成适当的类型。请参阅本
  表,看看你能指望得到什么。

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.

更新:我跑了使用各种咸菜协议测试( 0-3 ),并发现它失败 0 1 ,但成功的 2 3


UPDATE: I ran tests using the various pickle protocols (0-3) and found that it fails for 0 and 1, but succeeds for 2 and 3.

下面是用来生成腌制数据蟒蛇code:

Here's the python code used to generate the pickled data:

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 code到unpickle它:

And the java code to unpickle it:

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.0 data.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.2 数据。3 运行,它成功。

这篇关于阅读Android的蟒蛇泡菜数据流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆