Json重新格式化MongoDB导出

时间:2018-07-05 15:56:16

标签: python json mongodb

我有一个python函数:

from collections import defaultdict
from csv import DictReader


def solution(csv_filename):
    by_subject_id = defaultdict(lambda: {
        'name': None,
        'dob': None,
        'gender': None,
        'visits': {},
        'samples': {}
    })

    with open(csv_filename) as f:
        dict_reader = DictReader(f)
        for row in dict_reader:
            non_empty = {k: v for k, v in row.items() if v}
            subject_id = non_empty['subid']  # must have to group by
            first_visit = non_empty.get('firstvisit')  # optional
            sample = non_empty.get('samplenumber')  # optional
            visit = non_empty.get('visitdate1')  # optional

            if first_visit:
                by_subject_id[subject_id].update({
                    'name': non_empty.get('name'),
                    'dob': non_empty.get('dob'),
                    'gender': non_empty.get('gender')
                })
            elif visit:
                by_subject_id[subject_id]['visits'][visit] = {
                    'age': non_empty.get('age'),
                    'visit_category': non_empty.get('visitcategory')
                }
            elif sample:
                by_subject_id[subject_id]['samples'][sample] = {
                    'completed_by': non_empty.get('completed_by'),
                    'label_on_sample': non_empty.get('label_on_sample')
                }
    return [{'subject_id': k, **v} for k, v in by_subject_id.items()]

将CSV转换为:

'subid,firstvisit,name,contact,dob,gender,visitdate1,age,visitcategory,samplenumber,label_on_sample,completed_by
1,12/31/11,Bob,,12/31/00,Male,,,,,,
1,,,,,,12/31/15,17,Baseline Visit,,,
1,,,,,,12/31/16,18,Follow Up Visit,,,
1,,,,,,12/31/17,18,Follow Up Visit,,,
1,,,,12/31/00,Male,,17,,XXX123,1,Sally
2,1/1/12,,,1/1/01,Female,,,,,,
2,,,,,,1/1/11,10,Baseline Visit,,,
2,,,,,,1/1/12,11,Follow Up Visit,,,
2,,,,,,1/1/13,12,Follow Up Visit,,,
2,,,,,,1/1/14,13,Follow Up Visit,,,
2,,,,,,1/1/15,14,Follow Up Visit,,,
2,,,,1/1/01,Female,,15,,YYY456,2,
2,,,,1/1/01,Female,,15,,ZZZ789,2,Sally'

对于像这样的Json:

[
    {
        "subject_id": "1",
        "name": "Bob",
        "dob": "12/31/00",
        "gender": "Male",
        "visits": {
            "12/31/15": {
                "age": "17",
                "visit_category": "Baseline Visit"
            },
            "12/31/16": {
                "age": "18",
                "visit_category": "Follow Up Visit"
            },
            "12/31/17": {
                "age": "18",
                "visit_category": "Follow Up Visit"
            }
        },
        "samples": {
            "XXX123": {
                "completed_by": "Sally",
                "label_on_sample": "1"
            }
        }
    },
    {
        "subject_id": "2",
        "name": null,
        "dob": "1/1/01",
        "gender": "Female",
        "visits": {
            "1/1/11": {
                "age": "10",
                "visit_category": "Baseline Visit"
            },
            "1/1/12": {
                "age": "11",
                "visit_category": "Follow Up Visit"
            },
            "1/1/13": {
                "age": "12",
                "visit_category": "Follow Up Visit"
            },
            "1/1/14": {
                "age": "13",
                "visit_category": "Follow Up Visit"
            },
            "1/1/15": {
                "age": "14",
                "visit_category": "Follow Up Visit"
            }
        },
        "samples": {
            "YYY456": {
                "completed_by": null,
                "label_on_sample": "2"
            },
            "ZZZ789": {
                "completed_by": "Sally",
                "label_on_sample": "2"
            }
        }
    }
]

用于导出到Mongodb。但是,一旦进入Mongodb,我就会发现访问和样本的变量字段名称使查询变得非常困难。我可以对我的函数进行哪些修改,以返回像这样的json:

[
    {
        "subject_id": "1",
        "name": "Maria",
        "dob": "1/1/00",
        "gender": "F",
        "visits": [
            {   "date": "1/1/18",
                "date_entered": "1/2/18",
                "entered_by": "Sally"
            },
            {   "date" : "1/2/18",
                "date_entered": "1/2/18",
                "entered_by": "Tim"
            }
        ],
        "samples": [
            {   "sample_id" : "XXX123",
                "collected_by": "Sally",
                "collection_date": "1/3/18"
            }
        ]
    },
    {
        "subject_id": "2",
        "name": "Bob",
        "dob": "1/2/00",
        "gender": "M",
        "visits": [
            {   "date" : "1/3/18",
                "date_entered": "1/4/18",
                "entered_by": "Tim"
            }
        ],
        "samples": [
            {   "sample_id": "YYY456",
                "collected_by": "Sally",
                "collection_date": "1/5/18"
            },
            {   "sample_id" : "ZZZ789",
                "collected_by": "Tim",
                "collection_date": "1/6/18"
            },
            {   "sample_id": "AAA123",
                "collected_by": "Sally",
                "collection_date": "1/7/18"
            }
        ]
    }
]

任何建议将不胜感激。

0 个答案:

没有答案