2

我习惯使用 JSON 和 Numpy 在 python 中存储数组、列表和字典,但我想使用 BSON,因为浮点数只占用 4 个字节,从而减少文件大小。

使用 Json,我执行以下操作:

import numpy
import json

a = numpy.random.rand(12).reshape((3,4))

with open('out.json', 'w') as out:
    json.dump(a.tolist(), out)

with open('out.json') as inp:
    b = numpy.array(json.load(inp))

print b

我没有找到一种明显的方法来对 BSON 做同样的事情。我试过这个:

import numpy
from bson import BSON

a = numpy.random.rand(12).reshape((3,4))

b = BSON.encode({'a': a.tolist()})

with open('out.bson', 'wb') as out:
    out.write(b)

with open('out.bson', 'rb') as inp:
    print BSON().decode(inp.read())

但是得到这个错误:

Traceback (most recent call last):
  File "apaga.py", line 12, in <module>
    print BSON().decode(inp.read())
  File "/usr/lib/python2.7/dist-packages/bson/__init__.py", line 539, in decode
    (document, _) = _bson_to_dict(self, as_class, tz_aware)
bson.errors.InvalidBSON: not enough data for a BSON document
4

1 回答 1

2

我安装的 BSON 版本不会如图所示导入,所以也许我使用的是不同的版本。要在导入后在 python 类型 help(bson) 中查看您的文档...

像这样的东西应该工作:

import bson
>>> a = numpy.random.rand(12).reshape((3,4))
>>> b = bson.dumps({'a':a.tolist()})
>>> print bson.loads(b)
{u'a': [[0.033390565943162254, 0.7282666963459123, 0.03719924011978737, 0.2664821209717694], [0.6145164300761253, 0.3662769247564551, 0.5973843055182299, 0.42908933503924207], [0.05901830243140804, 0.31533731904861184, 0.7158207045507905, 0.12686922689849378]]}
于 2012-09-04T23:49:24.580 回答