我是 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/