How to organize the output that Python returns in the Pymongo module?

Asked

Viewed 49 times

-3

I am trying to return database and more functions. Ex:

mongodb = MongoClient(args.ip, int(porta))
database = mongodb.list_database_names()
print (database)

But when the answer comes back, it comes like this

['admin', 'config', 'local']

How can I organize the response? Ex:

    admin
    config
    local

etc....

no brackets, quotes and commas...? Thank you.

1 answer

1


What you get is a list of Databases names, so it’s normal for the object representation to come with brackets and commas.

If you want to have a different output, you can go through all the names in the list using a block for and print each name. See the example below:

mongodb = MongoClient(args.ip, int(porta))
databases = mongodb.list_database_names()

print("Databases:\n")

for name in databases:
    print("-", name)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.