如何获得没有括号的格式CSV和来自python脚本的u字母

时间:2016-10-26 20:46:15

标签: mongodb python-2.7 csv

如何删除python脚本中的u字母?当我运行以下脚本时,它会将数据从Mongodb导出到CSV,但它会在CSV场中显示你的字母。我试图使用许多不起作用的解决方案。请有人可以帮助我。

python脚本示例:

import codecs
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.en':1, 'stages.stageId':1 })

    with codecs.open('stages2.csv', 'w', encoding='utf-8') as outfile:
        fields = ['_id', 'stages.interview', 'stages.hmNotification', 'stages.hmStage', 'stages.type', 'stages.isEditable','stages.order', 'stages.name', 'stages.stageId']
        write = csv.DictWriter(outfile, fieldnames=fields)
        write.writeheader()
        for stages_record in cursor:
            stages_record_id = stages_record['_id']
            for stage_record in stages_record['stages']:
                x = {
                    '_id': stages_record_id,
                    'stages.interview': stage_record['interview'],
                    'stages.hmNotification': stage_record['hmNotification'],
                    'stages.hmStage': stage_record['hmStage'], 
                    'stages.type': stage_record.get('type'),                
                    'stages.isEditable': stage_record['isEditable'],
                    'stages.order': stage_record['order'],
                    'stages.name': stage_record['name'],
                    'stages.stageId': stage_record['stageId']}            
                write.writerow(x)

csv的输出不应该有'{u'en':u'“,括号'。如何修复python脚本?请帮助我。

CSV样本:

ID,stages.interview,stages.hmNotification,stages.hmStage,stages.type,stages.isEditable,stages.order,stages.name,stages.stageId     5318cbd9a377f52a6a0f671f,False,False,False,new,False,0,{u'en':u'New'},51d1a2f4c0d9887b214f3694

0 个答案:

没有答案