Python cursor=collection.find_one({quot;name";:“rain";})pymongo查询
这个查询返回游标对象,现在我想把它的结果存储在字符串中,告诉我怎么做,在python中是否可行。 我试过了Python cursor=collection.find_one({quot;name";:“rain";})pymongo查询,python,pymongo,Python,Pymongo,这个查询返回游标对象,现在我想把它的结果存储在字符串中,告诉我怎么做,在python中是否可行。 我试过了 obj=next(cursor,None) values=obj['values'] 但这就产生了错误 TypeError: 'dict' object is not an iterator AttributeError: 'str' object has no attribute 'iteritems' 当我试着去做 for i in cursor: string=i
obj=next(cursor,None)
values=obj['values']
但这就产生了错误
TypeError: 'dict' object is not an iterator
AttributeError: 'str' object has no attribute 'iteritems'
当我试着去做
for i in cursor:
string=i
j=j+1
for key,value in i.iteritems():
temp=[key,value]
string2.append(temp)
这返回了错误
TypeError: 'dict' object is not an iterator
AttributeError: 'str' object has no attribute 'iteritems'
首先,我不清楚这是dict对象还是str对象,我的主要目的是将此对象的内容复制到字符串中,以便在程序中使用它该方法从数据库返回单个文档,而不是a,这就是为什么在第一种情况下会出现
TypeError
。也就是说,您得到了属性错误,因为i
是字典中的键值,因此字符串和字符串没有任何名为'iteritems'
的属性。你想要的是。我的pymongo版本是3.0.3