Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

from pymongo.objectid import ObjectId ImportError: No module named objectid

I made an python code that fetch tweets from Mongo collection called Tweets. I wan't to fetch only the object text and add an additional object called Sentiment.

When i loop through the Tweets and parse the json object to a string i get the error :

from pymongo.objectid import ObjectId ImportError: No module named objectid

Therefor i use the following code

import pymongo
import nltk
import json
from json import JSONEncoder
from pymongo import MongoClient
from pymongo.objectid import ObjectId

#JSON Encoder
class MongoEncoder(JSONEncoder):
    def default(self, obj, **kwargs):
        if isinstance(obj, ObjectId):
            return str(obj)
        else:            
            return JSONEncoder.default(obj, **kwargs)

#Mongo Settings
client = MongoClient()
db = client.Sentiment
Tweets = db.Tweet
TweetTraining = db.TweetTraining

#GET TEXT_TAG FROM TWEET
for tweet in Tweets.find({"lang":"nl"},{"text"}):
  print json.dumps(tweet, cls=MongoEncoder)

I hope that you can help me out. Many thanks

Erik

like image 268
Erik hoeven Avatar asked Jan 31 '15 12:01

Erik hoeven


1 Answers

One of the imports at the top of your file is incorrect. ObjectId should be loaded from bson.objectid instead of pymongo.objectid

from bson.objectid import ObjectId

Here is a link to the pymongo documentation for querying by ObjectId

PyMongo - Querying By ObjectId

like image 104
Kevin Brady Avatar answered Oct 15 '22 11:10

Kevin Brady