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

标签 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'

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

最佳答案

您使用的是 Python 2.6 或更早版本。 OrderedDict 直到 2.7 版才添加到 Python。

来自documentation :

New in version 2.7.

你可以使用 this backport相反(也可从 PyPI 获得),它将适用于 python 2.4 及更高版本,或者安装 python 2.7 并使用该版本运行脚本。

关于python - 'module'对象在pymongo中没有属性 'OrderedDict'错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13586591/

相关文章:

java - Java中的HashMap和Hashtable有什么区别?

python - 执行功能的算法仅占所有情况的 12%

python - Scrapy:所有的爬虫都失败了。有语法错误的爬虫

python - 从python中的.ini文件读取特殊字 rune 本

javascript - Mongoose 保存卡住(回调从未调用)

C# 喜欢 VBA 中的 List<T>

python - 给定一个列表列表,如何在没有 pandas 的情况下使用 python 获取一组像 output_1 这样的列表?

java - 如何使用 Java 驱动程序访问嵌套在 MongoDB 数组中的对象

mongodb - 查找并删除 mongo/mongoid 中的所有内容

.net - 迭代列表并删除 .NET 中的其他成员