'module'对象在pymongo中没有属性'OrderedDict'错误

时间:2012-11-27 14:39:19

标签: python mongodb collections runtime-error pymongo

我对pymongo很新。在以下代码中,

db = MySQLdb.connect(DB_HOST,DB_USR,DB_PWD,DB_NAME)
cursor = db.cursor()
query = "SELECT * FROM %s WHERE userid = \"%s\"" % (table, userID) 
cursor.execute(query)
colNames = [i[0] for i in cursor.description]
rows = cursor.fetchall()
objects_list = []
# The below logic makes JSON objet based on fetch MySQL rows.
for row in rows:
    d = collections.OrderedDict()
    index = 0
    for col in colNames:
        d[col] = row[index]
        index = index + 1
    objects_list.append(d)
return objects_list

我收到了错误,

trngl_advertise_perfm
trngl_advertise_activity
trngl_user_fblike
Traceback (most recent call last):
File "IngestDataToMongo.py", line 83, in <module>
userData = getData(user[0], TABLES[i]) # Get data of each user.
File "IngestDataToMongo.py", line 51, in getData
d = collections.OrderedDict()
AttributeError: 'module' object has no attribute 'OrderedDict'

请告诉我,如何删除错误。

1 个答案:

答案 0 :(得分:11)

您使用的是Python 2.6或更早版本。在版本2.7之前,OrderedDict未添加到Python。

来自documentation

  

版本2.7中的新内容。

您可以使用this backport(也可以从PyPI获得),它可以在python版本2.4及更高版本上运行,或者安装python 2.7并使用该版本运行脚本。