简体   繁体   中英

Remove u' from a mongodb list

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)

The result is :

[{u'domain': u'allegacyhsa.org'}, {u'domain': u'americanhelpinghands.org'}]

I want to print only the domain like :

['allegacyhsa.org','americanhelpinghands.org']

Without u and {

Thank you in advance for your answer.

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"

Your question is about extracting the domain values , not about "removing the u". That question is simple to answer:

domains = [d['domain'] for d in data]

If you now want to format them for printing, you could for example use join :

print(', '.join(domains))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM