在 python 3 中将 bson objectid 传递给 numpy recarray 时遇到问题
Trouble with passing bson objectid to numpy recarray in python 3
我正在使用机器翻译一些存储在 mongodb 数据库中的文本。我正在尝试从数据库中提取数据,然后将其存储在 numpy recarray 中。但是,当我尝试将 ObjectId 字段保存到 recarray 时,我不断收到错误消息——尽管我已经阅读了不同的类型转换等。这是我的代码。任何建议都会有所帮助。
#Pull the records from the DB into a resultset
db_results_records_to_translate = \
db_connector.db_fetch_untranslated_records_from_db(
article_collection,rec_number)
#Create an empty numpy recarray to store the data
data_table_for_translation=np.zeros([db_results_records_to_translate.count(),6],
dtype=[('_id', np.str),
('article_raw_text', np.str),
('article_raw_date', np.str),
('translated',np.bool),
('translated_text',np.str),
('translated_date',np.str)])
#Write record data to the recarray
for index, r in enumerate(db_results_records_to_translate):
data_table_for_translation[index, 0] = str(r['_id']) # Line with errors!!!
data_table_for_translation[index,1] = r['article_raw_text']
data_table_for_translation[index,2] = r['article_raw_date']
data_table_for_translation[index, 3] = r['translated']
所以在 运行 这段代码之后,我得到一个错误 TypeError: expected an object with a buffer interface.
现在我尝试使用文档中引用的 str(ObjectId)
函数将 objectid 从 bson 转换为字符串,但没有成功。
有什么建议吗?
注意:我注意到即使是非 id 列也会发生此错误,因此即使是纯文本也有问题。
数组的定义有错误,包括数据类型,在迭代过程中索引字段有错误。
这个剪辑说明了我认为你需要做的改变才能让这个作业生效:
# fake data - a list of tuples
db_results_records_to_translate = [('12','raw text','raw date')]
#Create an empty numpy recarray to store the data
data_table_for_translation=np.zeros([1,],
dtype=[('_id', 'U10'),
('article_raw_text', 'U10'),
('article_raw_date', 'U10')])
# string dtype has to include length
# I'm using unicode here (Python3), 'S10' would do just as well (in py2)
#Write record data to the structured array
for index, r in enumerate(db_results_records_to_translate):
data_table_for_translation[index]['_id'] = str(r[0])
data_table_for_translation[index]['article_raw_text'] = r[1]
data_table_for_translation[index]['article_raw_date'] = r[2]
print(db_results_records_to_translate)
请注意,我按名称而不是编号对 'fields' 进行了索引。 data_table...
是具有 n 个字段的一维数组,而不是具有 n 列的二维数组。我按数字索引 r
因为我的模拟数据是一个元组,而不是数据库命名字段。
我正在使用机器翻译一些存储在 mongodb 数据库中的文本。我正在尝试从数据库中提取数据,然后将其存储在 numpy recarray 中。但是,当我尝试将 ObjectId 字段保存到 recarray 时,我不断收到错误消息——尽管我已经阅读了不同的类型转换等。这是我的代码。任何建议都会有所帮助。
#Pull the records from the DB into a resultset
db_results_records_to_translate = \
db_connector.db_fetch_untranslated_records_from_db(
article_collection,rec_number)
#Create an empty numpy recarray to store the data
data_table_for_translation=np.zeros([db_results_records_to_translate.count(),6],
dtype=[('_id', np.str),
('article_raw_text', np.str),
('article_raw_date', np.str),
('translated',np.bool),
('translated_text',np.str),
('translated_date',np.str)])
#Write record data to the recarray
for index, r in enumerate(db_results_records_to_translate):
data_table_for_translation[index, 0] = str(r['_id']) # Line with errors!!!
data_table_for_translation[index,1] = r['article_raw_text']
data_table_for_translation[index,2] = r['article_raw_date']
data_table_for_translation[index, 3] = r['translated']
所以在 运行 这段代码之后,我得到一个错误 TypeError: expected an object with a buffer interface.
现在我尝试使用文档中引用的 str(ObjectId)
函数将 objectid 从 bson 转换为字符串,但没有成功。
有什么建议吗?
注意:我注意到即使是非 id 列也会发生此错误,因此即使是纯文本也有问题。
数组的定义有错误,包括数据类型,在迭代过程中索引字段有错误。
这个剪辑说明了我认为你需要做的改变才能让这个作业生效:
# fake data - a list of tuples
db_results_records_to_translate = [('12','raw text','raw date')]
#Create an empty numpy recarray to store the data
data_table_for_translation=np.zeros([1,],
dtype=[('_id', 'U10'),
('article_raw_text', 'U10'),
('article_raw_date', 'U10')])
# string dtype has to include length
# I'm using unicode here (Python3), 'S10' would do just as well (in py2)
#Write record data to the structured array
for index, r in enumerate(db_results_records_to_translate):
data_table_for_translation[index]['_id'] = str(r[0])
data_table_for_translation[index]['article_raw_text'] = r[1]
data_table_for_translation[index]['article_raw_date'] = r[2]
print(db_results_records_to_translate)
请注意,我按名称而不是编号对 'fields' 进行了索引。 data_table...
是具有 n 个字段的一维数组,而不是具有 n 列的二维数组。我按数字索引 r
因为我的模拟数据是一个元组,而不是数据库命名字段。