在命令行中使用填充JSON文件蒙戈数据库在Python脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在命令行中使用填充JSON文件蒙戈数据库在Python脚本相关的知识,希望对你有一定的参考价值。

page = open("npm.json", "r")
parsed = json.loads(page.read())

for i in parsed["dependencies"]:
    collection.insert_one(i)

你好,我想在一个JSON文件的读取和填充我与所谓ependencies.it一直给我和错误的行MongoDB的。我曾尝试插入,insert_one&insert_many无济于事。

以下是错误我得到

    File "database.py", line 43, in <module>
    collection.insert_one(i)
  File "/usr/local/lib/python3.6/dist-packages/pymongo/collection.py", line 684, in insert_one
    common.validate_is_document_type("document", document)
  File "/usr/local/lib/python3.6/dist-packages/pymongo/common.py", line 453, in validate_is_document_type
    "collections.MutableMapping" % (option,))
TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
  File "database.py", line 43, in <module>
    collection.insert_one(i)
  File "/usr/local/lib/python3.6/dist-packages/pymongo/collection.py", line 684, in insert_one
    common.validate_is_document_type("document", document)
  File "/usr/local/lib/python3.6/dist-packages/pymongo/common.py", line 453, in validate_is_document_type
    "collections.MutableMapping" % (option,))
TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping

JSON文件是节点的依赖关系的列表。我跑了一个节点项目的命令列表NPM。任何人可以帮助在此先感谢

答案

它看起来像你的dependencies数据是dictionary。这意味着当您遍历它,你会得到的字符串(这是在这种情况下键)。

一个例子是,像这样:

my_entry = 'a': 1, 'b': 2

for i in myentry:
    print(i)

# will print 'a' then 'b', not 1 then 2

我假设每个依赖是一个关键,你要插入的值。所以

# dictionary.values() will give you just the values
for v in parsed['dependencies'].values():
    if isinstance(v, dict):
        collection.insert_one(v)
    else:
        raise ValueError("object is not dictionary")

蒙戈只需要jsonbson条目,这是像字典。当你通过keys,你想插入式string,它不支持的对象

以上是关于在命令行中使用填充JSON文件蒙戈数据库在Python脚本的主要内容,如果未能解决你的问题,请参考以下文章

使用命令行中的值填充现有 LibreOffice 文档

怎么在命令行中查看nodejs和npm的版本号

本地安装的 gulp 不在命令行中运行?

ExtJs:从 Json 响应中填充 ComboBox

使用 excel 行中的数据自动填充单词。每个 Excel 行的自动填充 Word 文档

nodejs:会话存储在哪里?啥是连接蒙戈?