0

How do I remove the u letter in the python script? when I run the following script, it exports data to CSV from Mongodb, but it brings u letters in CSV fiel. I tried to use many solution that did not work. please someone can help me.

the sample of python script :

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)

The output of csv should not have '{u'en':u'", brackets'. how do I fix the python script? please help me.

the sample of 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

user7070824
  • 133
  • 1
  • 2
  • 11
  • 1
    That's a `u'unicode string'` key-value pair in a dictionary `{}`. Have you considered reading through a tutorial? – jonrsharpe Oct 26 '16 at 20:47
  • i added encoding ='utf-8' in the script. it did not work. how can I change the script? – user7070824 Oct 26 '16 at 20:50
  • See http://sopython.com/wiki/What_tutorial_should_I_read%3F, and [ask]. – jonrsharpe Oct 26 '16 at 20:51
  • someone can help me about the csv format issue? I need to import the csv file into PostgreSQL database. – user7070824 Oct 27 '16 at 03:56
  • Possible duplicate of [how to remove u letter and show the value of sub fieild only in python](http://stackoverflow.com/questions/40267590/how-to-remove-u-letter-and-show-the-value-of-sub-fieild-only-in-python) – Kennet Celeste Oct 27 '16 at 07:07

0 Answers0