frazman
frazman

Reputation: 33243

convert from unicode to ascii python

Ok.. This might be the duplicate.. but I dont think I even know the right problem. I am guessing I have a string in unicode.. (basically i am reading from mongo db.. and mongodb stores everything in that form???? Honestly I am not sure.. but this is what I get..

{ u'preview': u'Hello World!!'}

so there is this u' in front of all the fields.. I am basically trying to extract these out!! and then append them in one giant string. so lets say I do something like:

 string =  ''
 resolve = foo['first_resolved_at']
 string += resolve

So it throws an error

TypeError: coercing to Unicode: need string or buffer, NoneType found

What am I doing wrong? I guess I have to convert it to a string.. but how??? Thanks

Upvotes: 2

Views: 2548

Answers (2)

jdi
jdi

Reputation: 92569

Even though the answer was accepted, I thought I would include for reference an example of how you should be using join() instead of adding strings together. This also shows you that its avoiding None values:

d = {u'a': u'a', u'c': None, u'b': u'b', u'e': None, u'd': None, u'g': u'g', u'f': u'f', u'i': u'i', u'h': u'h', u'k': u'k', u'j': u'j', u'm': None, u'l': u'l', u'o': u'o', u'n': None, u'p': u'p'}

resolve = ''.join((value for value in d.itervalues() if value is not None))
print resolve
# u'abgfihkjlop'

And if what you wanted to do was only loop over a predetermined set of keys:

keys = ('c', 'g', 'f', 'm')
''.join([v for v in (d[k] for k in keys) if v is not None])

Here is a test showing the difference between this approach, appending to a list, and adding strings together:

from time import time

d = {}
for i in xrange(1000):
    v = u'v%d' % i
    d[v] = v

def test1():
    return ''.join(v for v in d.itervalues() if v is not None)

def test2():
    result = []
    for v in d.itervalues():
        if v is not None:
            result.append(v)
    return ''.join(result)

def test3():
    result = ''
    for v in d.itervalues():
        if v is not None:
            result += v
    return result

def timeit(fn):
    start = time()
    r = fn()
    end = time() - start
    print "Sec:", end, "msec:", end*1000


>>> timeit(test1)
Sec: 0.000195980072021 msec: 0.195980072021
>>> timeit(test2)
Sec: 0.000204086303711 msec: 0.204086303711
>>> timeit(test3)
Sec: 0.000397920608521 msec: 0.397920608521

You can see that when your loops get bigger it really makes a difference.

Upvotes: 2

Johan Lundberg
Johan Lundberg

Reputation: 27028

Unicode to ascii is just str(some_unicode_string)

This { u'preview': u'Hello World!!'}

is a map. Loop over it to get the contents:

keys=[]
values=[]
for i in mymap:
    print i
    print "contains:",mymap[i]
    keys.append(i)
    values.append(i)

or just mymap.keys() and mymap.values()

So I think you should not convert your strings at all. But if you want to then just str(q)

EDIT:

Now your example code after the edits works. I hope this was useful.

Upvotes: 1

Related Questions