pymongo BSON 公共 API 未导入 Jupyter Lab

问题描述 投票:0回答:0

我正在做一个需要我将 BSON 数据集导入 Pandas 的项目。我正在尝试使用

bson.decode_all
方法来这样做。

我有一个名为“tf”的conda环境,安装了

pymongo
。这是我在 Jupyter 中使用
(tf) PS C:\Users\ashka\Desktop\spring 23\RSRC 4033\cybersecurity tweets\jupyter> jupyter lab
:

打开的当前脚本
import os
import pprint
from platform import python_version

import pandas as pd
import bson
import tensorflow as tf

print(python_version())
print(bson.__file__)
print(bson.__all__)

给:

3.9.16
C:\Users\ashka\anaconda3\envs\tf\lib\site-packages\bson\__init__.py
['loads', 'dumps']

# preprocessing
data_file = "../dataset/threat/tweets.bson"
with open(data_file, 'rb') as f:
    dataset_dict = bson.decode_all(f.read())
pprint.pprint(dataset_dict)

给:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[35], line 4
      2 data_file = "../dataset/threat/tweets.bson"
      3 with open(data_file, 'rb') as f:
----> 4     dataset_dict = bson.decode_all(f.read())
      5 pprint.pprint(dataset_dict)

AttributeError: module 'bson' has no attribute 'decode_all'

如您所见,导入的 bson 的唯一属性是“加载”和“转储”。有趣的是,在 SAME 环境下,这似乎不是 Jupyter 之外的问题。我在同一目录中创建了一个新的 .py 文件并使用

(tf) PS C:\Users\ashka\Desktop\spring 23\RSRC 4033\cybersecurity tweets\jupyter> python .\bsontest.py
:

运行它
from platform import python_version

import bson

print(python_version())
print(bson.__file__)
print(bson.__all__)

它给了:

3.9.16
C:\Users\ashka\anaconda3\envs\tf\lib\site-packages\bson\__init__.py
['ALL_UUID_SUBTYPES', 'CSHARP_LEGACY', 'JAVA_LEGACY', 'OLD_UUID_SUBTYPE', 'STANDARD', 'UUID_SUBTYPE', 'Binary', 'UuidRepresentation', 'Code', 'DEFAULT_CODEC_OPTIONS', 'CodecOptions', 'DBRef', 'Decimal128', 'InvalidBSON', 'InvalidDocument', 'InvalidStringData', 'Int64', 'MaxKey', 'MinKey', 'ObjectId', 'Regex', 'RE_TYPE', 'SON', 'Timestamp', 'utc', 'EPOCH_AWARE', 'EPOCH_NAIVE', 'BSONNUM', 'BSONSTR', 'BSONOBJ', 'BSONARR', 'BSONBIN', 'BSONUND', 'BSONOID', 'BSONBOO', 'BSONDAT', 'BSONNUL', 'BSONRGX', 'BSONREF', 'BSONCOD', 'BSONSYM', 'BSONCWS', 'BSONINT', 'BSONTIM', 'BSONLON', 'BSONDEC', 'BSONMIN', 'BSONMAX', 'get_data_and_view', 'gen_list_name', 'encode', 'decode', 'decode_all', 'decode_iter', 'decode_file_iter', 'is_valid', 'BSON', 'has_c', 'DatetimeConversion', 'DatetimeMS']

包含我需要的一切!!!

为什么

bson
模块在 Jupyter 中的行为不同,我该如何修复它?

pymongo jupyter-lab bson
© www.soinside.com 2019 - 2024. All rights reserved.