用Python操作大型JSON

2024-10-02 00:35:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个相当大的.json格式的数据文件,我想处理它,它的格式如下,就像许多json对象放在一起一样:

[
{ 
    "_id" : "...", 
    "idSession" : "...", 
    "createdAt" : "1526894989268", 
    "status" : "COMPLETE", 
    "raw" : "Bobsguide,Marketing Assistant,Sales / Marketing79642,Baitshepi,,etc", 
    "updatedAt" : "...", 
    "graphResults" : [

        [
            "lastName", 
            "stock"
        ], 
        [
            "country", 
            "Botswana"
        ], 
        [
            "location", 
            "Botswana  "
        ], 
        [
            "city", 
            "-"
        ], 
        [
            "state", 
            "-"
        ], 
        [
            "school", 
            "Heriot-Watt University"
        ], 
        [
            "skills", 
            "Budgeting,Business Process Improvement,Business Planning"
        ], 

    ], 

    "eid" : {
        "###" : "12020653-1889-35be-8009-b1c9d43768ac"
    }
}

{ 
    "_id" : "...", 
    "idSession" : "...", 
    "createdAt" : "1526894989268", 
    "status" : "COMPLETE", 
    "raw" : "Bobsguide,79619,Steven,example,steven.jones@example.com,Marketing Assistant,Sales,,etc", 
    "updatedAt" : "...", 
    "graphResults" : [
        [
            "country", 
            "United Kingdom"
        ], 
        [
            "location", 
            "United Kingdom London London"
        ], 
        [
            "city", 
            "London"
        ], 
        [
            "state", 
            "London"
        ], 
        [
            "skills", 
            "Solvency II,Liquidity Risk,Screening,etc"
        ]
    ], 

    "eid" : {
        "###" : "..."
    }
}

...



]

有没有一种简单的方法可以让我将其读入python脚本中进行操作/分析。感兴趣的主要部分是图形结果和原始数据的标签不足。我对这种形式的原始数据缺乏经验,所以非常感谢您的帮助。你知道吗


Tags: idjsonraw格式statusetcmarketingcomplete
1条回答
网友
1楼 · 发布于 2024-10-02 00:35:38

首先,你发布的数据不正确,应该是下面这样的内容,要访问你提到的元素可以尝试下面的内容

{
    "test":[
    { 
        "_id" : "...", 
        "idSession" : "...", 
        "createdAt" : "1526894989268", 
        "status" : "COMPLETE", 
        "raw" : "Bobsguide,Marketing Assistant,Sales /             Marketing79642,Baitshepi,,etc", 
        "updatedAt" : "...", 
        "graphResults" : [

            [
                "lastName", 
                "stock"
            ], 
            [
                "country", 
                "Botswana"
            ], 
            [
                "location", 
                "Botswana  "
            ], 
            [
                "city", 
                "-"
            ], 
            [
                "state", 
                "-"
            ], 
            [
                "school", 
                "Heriot-Watt University"
            ], 
            [
                "skills", 
                "Budgeting,Business Process Improvement,Business Planning"
            ]
        ], 
        "eid" : {
            "###" : "12020653-1889-35be-8009-b1c9d43768ac"
        }
        },
        { 
            "_id" : "...", 
            "idSession" : "...", 
            "createdAt" : "1526894989268", 
            "status" : "COMPLETE", 
            "raw" : "Bobsguide,79619,Steven,example,steven.jones@example.com,Marketing     Assistant,Sales,,etc", 
            "updatedAt" : "...", 
            "graphResults" : [
                [
                    "country", 
                    "United Kingdom"
                ], 
                [
                    "location", 
                    "United Kingdom London London"
                ], 
                [
                    "city", 
                    "London"
                ], 
                [
                    "state", 
                    "London"
                ], 
                [
                    "skills", 
                    "Solvency II,Liquidity Risk,Screening,etc"
                ]
            ], 

            "eid" : {
                "###" : "..."
            }
        }
    ]
}

//回答

import json

data_file = open('data.json', 'r')
information = json.load(data_file) // this will give you a json obj

print(information['test'][1]['raw']) // would pick element 1 from array then 

选取并打印原始关键字中的值

print(information['test'][1]['graphResults']) // would pick element 1 from array then pick and print value in raw key

相关问题 更多 >

    热门问题