Remove u' from a mongodb list

Lio*_*elF 0 python mongodb

I have a script and I want to remove u'domain' and u from the list

from pymongo import MongoClient


client = MongoClient()
db = client.domains
collection = db.domain
find_document = collection.find({},{'domain': 1, '_id':0})

data = list(find_document.limit(2))

{myIds.push(myDoc.domain.str)})
print (data)
Run Code Online (Sandbox Code Playgroud)

The result is :

[{u'domain': u'allegacyhsa.org'}, {u'domain': u'americanhelpinghands.org'}]
Run Code Online (Sandbox Code Playgroud)

I want to print only the domain like :

['allegacyhsa.org','americanhelpinghands.org']
Run Code Online (Sandbox Code Playgroud)

Without u and {

Thank you in advance for your answer.

Cyr*_*bil 5

The u'' notation indicates that you are getting unicode strings (Official documentation). You can either:

  • pick an encoding when printing (ie: data[0]['domain'].encode('utf8'))
  • cheat with json: print(json.dumps(data, indent=2))

From pymongo doc:

MongoDB stores data in BSON format. BSON strings are UTF-8 encoded so PyMongo must ensure that any strings it stores contain only valid UTF-8 data. Regular strings () are > validated and stored unaltered. Unicode strings () are encoded UTF-8 first. > The reason our example string is represented in the Python shell as u’Mike’ instead of ‘Mike’ is that PyMongo decodes each BSON string to a Python unicode string, not a regular str."

To retrieve only the domains, convert your data to a list of domain like so:

domain_list = [datum.get('domain') for datum in data]

print(', '.join(domain_list))
# prints: allegacyhsa.org, americanhelpinghands.org

print(', '.join('%r' % d for d in domain_list))
# prints: "allegacyhsa.org", "americanhelpinghands.org"
Run Code Online (Sandbox Code Playgroud)