我在从mongodb将数据导出到csv时,遇到python脚本中缺少字段名称的问题。类型字段名称存在于第一条记录中,但它不会出现在其余记录中。如果编写python脚本,如果它不存在,则为类型字段提供空值。从mongodb导出json数据到csv
的MongoDB集合的样本:
"stages": [
{
"interview": false,
"hmNotification": false,
"hmStage": false,
"type": "new",
"isEditable": false,
"order": 0,
"name": {
"en": "New"
},
"stageId": "51d1a2f4c0d9887b214f3694"
},
{
"interview": false,
"hmNotification": true,
"isEditable": true,
"order": 1,
"hmStage": true,
"name": {
"en": "Pre-Screen"
},
"stageId": "51f0078d7297363f62059699"
},
{
"interview": false,
"hmNotification": false,
"hmStage": false,
"isEditable": true,
"order": 2,
"name": {
"en": "Phone Screen"
},
"stageId": "51d1a326c0d9887721778eae"
}]
Python脚本的示例:
import csv
cursor = db.workflows.find({}, {'_id': 1, 'stages.interview': 1, 'stages.hmNotification': 1, 'stages.hmStage': 1, 'stages.type':1, 'stages.isEditable':1, 'stages.order':1,
'stages.name':1, 'stages.stageId':1 })
flattened_records = []
for stages_record in cursor:
stages_record_id = stages_record['_id']
for stage_record in stages_record['stages']:
flattened_record = {
'_id': stages_record_id,
'stages.interview': stage_record['interview'],
'stages.hmNotification': stage_record['hmNotification'],
'stages.hmStage': stage_record['hmStage'],
'stages.type': stage_record['type'],
'stages.isEditable': stage_record['isEditable'],
'stages.order': stage_record['order'],
'stages.name': stage_record['name'],
'stages.stageId': stage_record['stageId']}
flattened_records.append(flattened_record)
运行python脚本时,它显示KeyError异常: “类型”。请帮助我如何在脚本中添加缺少的字段名称。
它工作时,增加了“stages.typ非常好‘:stage_record.get。在python脚本(’类型”)类型的值出现在CSV文件。非常感谢。 – user7070824
感谢您的帮助。如何删除u字母,括号和Python中子字段的名称?谢谢。 – user7070824