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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With