【问题标题】:iterate over a Json file and insert into db in Python遍历一个 Json 文件并在 Python 中插入到 db
【发布时间】:2019-03-08 00:24:35
【问题描述】:
{"groupId":"org.springframework","artifactId":"spring-jcl","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jcl/5.1.2.RELEASE/f0d7165b6cfb90356da4f25b14a6437fdef1ec8a/spring-jcl-5.1.2.RELEASE.jar","dependencies":[]}]}]},
{"groupId":"org.springframework","artifactId":"spring-beans","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/5.1.2.RELEASE/5d513701a79c92f0549574f5170a05c4af7c893d/spring-beans-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-core","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/5.1.2.RELEASE/b9b00d4075c92761cfd4e527e0bdce1931b4f3dc/spring-core-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-jcl","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jcl/5.1.2.RELEASE/f0d7165b6cfb90356da4f25b14a6437fdef1ec8a/spring-jcl-5.1.2.RELEASE.jar","dependencies":[]}]}]},
{"groupId":"org.springframework","artifactId":"spring-expression","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-expression/5.1.2.RELEASE/3c16b062785e4c101db6b754fcb34a77c1e912c/spring-expression-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-core","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/5.1.2.RELEASE/b9b00d4075c92761cfd4e527e0bdce1931b4f3dc/spring-core-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-jcl","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jcl/5.1.2.RELEASE/f0d7165b6cfb90356da4f25b14a6437fdef1ec8a/spring-jcl-5.1.2.RELEASE.jar","dependencies":[]}]}]},
{"groupId":"org.springframework","artifactId":"spring-core","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/5.1.2.RELEASE/b9b00d4075c92761cfd4e527e0bdce1931b4f3dc/spring-core-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-jcl","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jcl/5.1.2.RELEASE/f0d7165b6cfb90356da4f25b14a6437fdef1ec8a/spring-jcl-5.1.2.RELEASE.jar","dependencies":[]}]}]},
{"groupId":"org.springframework","artifactId":"spring-web","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-web/5.1.2.RELEASE/3ff2a93b072da42c3930225e3dceeabb0678eb0b/spring-web-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-beans","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/5.1.2.RELEASE/5d513701a79c92f0549574f5170a05c4af7c893d/spring-beans-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-core","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/5.1.2.RELEASE/b9b00d4075c92761cfd4e527e0bdce1931b4f3dc/spring-core-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-jcl","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jcl/5.1.2.RELEASE/f0d7165b6cfb90356da4f25b14a6437fdef1ec8a/spring-jcl-5.1.2.RELEASE.jar","dependencies":[]}]}]},
{"groupId":"org.springframework","artifactId":"spring-core","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/5.1.2.RELEASE/b9b00d4075c92761cfd4e527e0bdce1931b4f3dc/spring-core-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-jcl","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jcl/5.1.2.RELEASE/f0d7165b6cfb90356da4f25b14a6437fdef1ec8a/spring-jcl-5.1.2.RELEASE.jar","dependencies":[]}]}]}]}]

output, error = process.communicate()
data = json.loads(output.decode('UTF-8'))

page = open("JSON/Java.json", "r")
parsed = json.loads(page.read())
for v in parsed['groupId'].values():
    if isinstance(v, dict):
        db_entry = {}
        db_entry['artifactId'] = v['artifactId']
        db_entry['version'] = v['version']
        db_entry['repo_id'] = repo_id
        dep_coll.insert_one(db_entry)
    else:
        raise ValueError("object is not dictionary")

嗨,我正在尝试在迭代时导入一个 json 文件,以便将某些值插入到数据库中。在这种情况下,我需要 artifactid,插入版本。 json 文件有一个列表,所以我会有多个插入。我认为我在正确的轨道上,任何帮助将不胜感激。目前这是我在 parsed['groupId'].values() 中遇到的错误: TypeError: 列表索引必须是整数或切片,而不是 str

【问题讨论】:

    标签: python mongodb list


    【解决方案1】:

    'parsed' 是一个列表,而不是一个字典。它包含字典。每个字典都包含一个名为“groupId”的字段。所以你的代码应该是这样的。

    如果以下代码不适合您,请将“JSON/Java.json”上传到公共位置。

    for entry in parsed:
        # entry is a dict - do something with it 
    

    【讨论】:

    • for v in data: db_entry = {} db_entry['name'] = v['artifactId'] db_entry['version'] = v['version'] db_entry['repo_id'] = repo_id dep_coll.insert_one(db_entry)
    • 谢谢,它有点工作,但只插入一个。我希望我可以插入多个,这就是为什么我使用 values()
    猜你喜欢
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 2020-07-21
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多