Python 使用 PyMongo 打印漂亮的 MongoDB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34598563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:15:02  来源:igfitidea点击:

MongoDB Print Pretty with PyMongo

pythonmongodbprintingpymongo

提问by Vandexel

I've looked up print pretty for MongoDB, and i understand how to do it from the shell. What I can't find is how to do it with PyMongo, so that when I run it in eclipse, the output will print pretty instead of all in one line. Here's what I have right now:

我已经查找了 MongoDB 的打印效果,并且我了解如何从 shell 执行此操作。我找不到的是如何用 PyMongo 来做,所以当我在 eclipse 中运行它时,输出会打印得很漂亮,而不是在一行中打印出来。这是我现在所拥有的:

  cursor = collection.find({})
  for document in cursor: print(document)

This prints everything in my collection, but each document in my collection just prints in one line. How can i change this to get it to print pretty?

这会打印我收藏中的所有内容,但我收藏中的每个文档只打印一行。我怎样才能改变它以使其打印得漂亮?

采纳答案by masnun

PyMongo fetches the documents as Python data structures. So you can use pprintwith it like this:

PyMongo 将文档作为 Python 数据结构获取。所以你可以pprint像这样使用它:

from pprint import pprint

cursor = collection.find({})
for document in cursor: 
    pprint(document)