1

I am trying to retrieve my data from MongoDB using Python but u get added to its key value.

Here is my program:

from pymongo import MongoClient
import json
import array
Data = MongoClient()
Variable = Data.Abhi
Dataset = []
i = 0
Outcome = Variable.Temp.find()

for Result in Outcome:
     Dataset.append(Result)
print Dataset

Output :

[{u'cuisine': u'Italian', u'borough': u'Manhattan', u'name': u'Vella', u'restaurant_id': u'41704620', u'grades': [{u'grade': u'A', u'score': 11}, {u'grade': u'B', u'score': 17}], u'address': {u'building': u'1480', u'street': u'2 Avenue', u'zipcode': u'10075', u'coord': [-73.9557413, 40.7720266]}, u'_id': ObjectId('576948b163a37b378dc21565')}]

I have checked using terminal with find() in my database it is showing

{ "_id" : ObjectId("576948b163a37b378dc21565"), "cuisine" : "Italian", "name" : "Vella", "restaurant_id" : "41704620", "grades" : [ { "grade" : "A", "score" : 11 }, { "grade" : "B", "score" : 17 } ], "address" : { "building" : "1480", "street" : "2 Avenue", "zipcode" : "10075", "coord" : [ -73.9557413, 40.7720266 ] }, "borough" : "Manhattan" }

Please help me where am I making mistake.

Thanks in advance.

scott_lotus
  • 3,171
  • 22
  • 51
  • 69
Abhishek Parikh
  • 949
  • 1
  • 20
  • 41
  • 3
    Python v2 uses u as prefix to indicate the string is unicode, the literal without the u should make you nervois, as then the content from the db wouöd have öost its encoding. Try print the strings and the u will disappear ;-) in Python v3 there is all unicode or expöicit bytes no more that bytestring. – Dilettant Jun 22 '16 at 05:57
  • As @Dilettant already pointed out, that's the [representation](http://stackoverflow.com/questions/7784148/understanding-repr-function-in-python) for unicode strings. Nothing to fear. By "using terminal with find()" do you mean the mongo shell written in javascript, which is quite different. – Ilja Everilä Jun 22 '16 at 06:03
  • Thanks both of you – Abhishek Parikh Jun 22 '16 at 06:15

1 Answers1

0

you can directly access this by using:

for Result in Outcome:

Result['your key']

Community
  • 1
  • 1
  • I think the problem has more to do with the unicode representation. Maybe adding to that explanation will make this answer better. As it stands, you're not explaining why the OP saw the prefix "u" and what that represents. – razdi Aug 23 '19 at 07:33