0

I already searched the solutions online, but it did not work on the following python script. I am getting u letter in csv file.

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import codecs
import csv
cursor = db.jobs.find( {}, {'_id':1,  'locationIds':1,  'termMap.en':1  })
with codecs.open('job_test.csv', 'w', encoding='utf-8') as outfile:
    fields = ['_id', 'locationIds',  'termMap.en']
    write = csv.DictWriter(outfile, fieldnames=fields)
    write.writeheader()
    for x in cursor:            
        x_id = x['_id']         
        x_locationIds =x.get('locationIds') 
        x_termMap =x['termMap'].get('en')                           
        z = {
            '_id': x_id,            
            'locationIds':str(x_locationIds).encode('ascii', 'ignore'), 
            'termMap.en':str(x_termMap).encode('ascii', 'ignore') }                        
        write.writerow(z)

the output is _

id  locationIds termMap.en
51dc52fec0d988a9547b5201    [u'00aaaaaaaaaaaaaaa5913490', u'00aaaaaaaaaaaaaaa6118158', u'00aaaaaaaaaaaaaaa5946768'] [u'abc', u'acuity', u'analyze', u'become', u'beverage']
51dc52fec0d988a9547b5202    [u'00aaaaaaaaaaaaaaa5946768']   [u'abc', u'air', u'angles', u'apples', u'banana', u'bananas', u'because', u'beings', u'birds', u'bottom']

I tried to use many different ways, but I can not remove the u letters yet. please someone can help me.

user7070824
  • 133
  • 1
  • 2
  • 11
  • 5
    Possible duplicate of [What's the u prefix in a python string](http://stackoverflow.com/questions/2464959/whats-the-u-prefix-in-a-python-string) – fuglede Dec 08 '16 at 20:07
  • 1
    Why are you trying to write a list into a CSV column? You are producing the `[..]` brackets and the `u` prefixes by calling `str(x_locationIds)`. – Martijn Pieters Dec 08 '16 at 20:07
  • Try using `str.encode('utf-8')` where `str` is the string you want to remove the u prefix from. – castle-bravo Dec 08 '16 at 20:09
  • Because I need to export data to csv and do data migration, I need to export to CSV file. I tried str.encode('utf-8'), it did not work. thanks, – user7070824 Dec 08 '16 at 20:14
  • You need to have a clear, precise idea of what the output columns *should* look like. You have a single column with a plural name "locationId**s**" so seemingly it is your intention to pack multiple items into a single column—how would you like this to look? Should the IDs be concatenated together directly, or maybe delimited with colons, or spaces, or what? The answer to this will determine how you convert your lists (e.g. `x_locationIds`) into strings—`str(x_locationIds)` is clearly not the right way. – jez Dec 08 '16 at 20:23
  • I want to remove the u letter and single quotation in the locationIds column as well as the termMap.en column. it looks like the following:_id locationIds termMap.en 51dc52fec0d988a9547b5201 [00aaaaaaaaaaaaaaa5913490, 00aaaaaaaaaaaaaaa6118158] [abc, acuity] thanks – user7070824 Dec 08 '16 at 20:32
  • I tried str(x_locationIds), it did not work. thanks. – user7070824 Dec 08 '16 at 20:35
  • @user7070824: we are trying to extract your requirements so we can help you. We know what you tried, we know that doesn't work. – Martijn Pieters Dec 08 '16 at 20:53
  • thanks, @ Martijn Pieters, – user7070824 Dec 08 '16 at 20:57

0 Answers0