Python 将MongoDB对象id导出到YAML文件

Python 将MongoDB对象id导出到YAML文件,python,mongodb,python-3.x,pymongo,Python,Mongodb,Python 3.x,Pymongo,我正在尝试将MongoDb对象id存储到yaml文件中,我还需要将其加载回去。但我目前正在努力将其导出到yaml文件中。我发现了一个例子,表明这是可以做到的。我试图遵循的例子如下 #我的变量名 集合\u插入\u id ObjectId('5923BB676CAB373C45012A1') #变量类型 类型(集合\插入\标识) #这是我下面例子的一个片段,它抛出了一个错误 def objectid_重新输入(转储程序、数据): 返回转储程序。表示\u标量(“!bson.objectid.objec

我正在尝试将MongoDb对象id存储到yaml文件中,我还需要将其加载回去。但我目前正在努力将其导出到yaml文件中。我发现了一个例子,表明这是可以做到的。我试图遵循的例子如下

#我的变量名
集合\u插入\u id
ObjectId('5923BB676CAB373C45012A1')
#变量类型
类型(集合\插入\标识)
#这是我下面例子的一个片段,它抛出了一个错误
def objectid_重新输入(转储程序、数据):
返回转储程序。表示\u标量(“!bson.objectid.objectid”,str(data))
yaml.SafeDumper.add\u representer(集合\u插入\u id,objectid\u representer)
yaml.safe\u dump(我的dict'data.yaml',默认\u flow\u style=False)
#这是我在通过第二行时遇到的错误。
文件“”,第1行,在
文件“/ws/mastarke-sjc/pyats2/lib/python3.4/site-packages/yaml/_-init__.py”,
第216行,在安全转储中
返回转储\所有([数据],流,转储程序=安全转储程序,**kwds)
文件“/ws/mastarke sjc/pyats2/lib/python3.4/site-
packages/yaml/_init__.py”,第188行,在dump_all中
dumper.represente(数据)
文件“/ws/mastarke sjc/pyats2/lib/python3.4/site-
包装/yaml/representer.py”,第26行,代表
节点=自身。表示_数据(数据)
文件“/ws/mastarke sjc/pyats2/lib/python3.4/site-
第47行中的packages/yaml/representer.py”表示_数据
node=self.yaml\u代表[data\u types[0]](self,data)
文件“/ws/mastarke sjc/pyats2/lib/python3.4/site-
文件包/yaml/representer.py”,第203行,以表示
返回self.representation_映射('tag:yaml.org,2002:map',data)
文件“/ws/mastarke sjc/pyats2/lib/python3.4/site-
packages/yaml/representer.py”,第116行,表示_映射
节点\u值=自身。表示\u数据(项目\u值)
文件“/ws/mastarke sjc/pyats2/lib/python3.4/site-
第57行中的packages/yaml/representer.py”表示_数据
node=self.yaml_represents[None](self,data)
文件“/ws/mastarke sjc/pyats2/lib/python3.4/site-
包装/yaml/representer.py”,第227行,表示未定义
raise REPRESENTERROR(“无法表示对象:%s”%data)
yaml.representer.RepresenterError:无法表示对象:
5923BB676CAB373C45012A1
#这是我试图遵循的代码,建议如何发送MongoDB
#yaml文件的对象id
def objectid_重新输入(转储程序、数据):
返回转储程序。表示\u标量(“!bson.objectid.objectid”,str(data))
yaml.SafeDumper.add_representer(objectid.objectid,objectid_representer)
yaml.safe\u dump(带有\u objectid、yamlfile、默认\u flow\u style=False的文档)

转换为json,然后是
yaml.dump(json.load())中的一个
。阅读文档中的免责声明。也许我有点困惑,但是当直接在MongoDB shell中工作时,您引用的文档似乎更倾向于导出。我通过Python3与Mongo进行接口,所以我正在寻找使用Python的解决方案。不,你不是。我不是在回答这个问题,而是建议使用可用的工具。如果您确实需要手动执行,您是否尝试过直接
yaml.dump
它?
# My variable name
collection_insert_id
ObjectId('5923bb676cab3d37c45012a1')

# variable type
type(collection_insert_id)
<class 'bson.objectid.ObjectId'>

# this is a snip of the example I am following, Its throwing an error
def objectid_representer(dumper, data):
    return dumper.represent_scalar("!bson.objectid.ObjectId", str(data))

yaml.SafeDumper.add_representer(collection_insert_id, objectid_representer)
yaml.safe_dump(my_dict, 'data.yaml',default_flow_style=False)


# This is the error I get when passing the second line.
File "<stdin>", line 1, in <module>
File "/ws/mastarke-sjc/pyats2/lib/python3.4/site-packages/yaml/__init__.py", 
line 216, in safe_dump
return dump_all([data], stream, Dumper=SafeDumper, **kwds)
  File "/ws/mastarke-sjc/pyats2/lib/python3.4/site-
packages/yaml/__init__.py", line 188, in dump_all
dumper.represent(data)
File "/ws/mastarke-sjc/pyats2/lib/python3.4/site-
packages/yaml/representer.py", line 26, in represent
node = self.represent_data(data)
File "/ws/mastarke-sjc/pyats2/lib/python3.4/site-
packages/yaml/representer.py", line 47, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "/ws/mastarke-sjc/pyats2/lib/python3.4/site-
packages/yaml/representer.py", line 203, in represent_dict
return self.represent_mapping('tag:yaml.org,2002:map', data)
File "/ws/mastarke-sjc/pyats2/lib/python3.4/site-
packages/yaml/representer.py", line 116, in represent_mapping
node_value = self.represent_data(item_value)
File "/ws/mastarke-sjc/pyats2/lib/python3.4/site-
packages/yaml/representer.py", line 57, in represent_data
node = self.yaml_representers[None](self, data)
File "/ws/mastarke-sjc/pyats2/lib/python3.4/site-
packages/yaml/representer.py", line 227, in represent_undefined
raise RepresenterError("cannot represent an object: %s" % data)
yaml.representer.RepresenterError: cannot represent an object: 
5923bb676cab3d37c45012a1


# This is the code I am trying to follow that suggest how to send MongoDB 
# object id to yaml file
def objectid_representer(dumper, data):
    return dumper.represent_scalar("!bson.objectid.ObjectId", str(data))

yaml.SafeDumper.add_representer(objectid.ObjectId, objectid_representer)
yaml.safe_dump(doc_with_objectid, yamlfile, default_flow_style=False)