convertir de unicode a 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

preguntado el 01 de febrero de 12 a las 22:02

I just tried this and it works fine. Please make an example that shows the actual error and post that - these frequent edits are a little crazy. -

I think there is some missing information here. foo is obviously some kind of django Query object or a pymongo wrapper that acts like a defaultdict, and returns None for missing keys. There is nothing wrong with adding an ascii and unicode (though in a loop its bad, you should join). resolve is obviously None and you need to check your values. -

yeah.. you guys are correct.. the problem was.. sometimes when there is no value in the db.. it returns None.. :( and trying to append that to string might be what is causing the probelm -

Honestly, unless you show more of your code, this might be a vote-down. You are doing something in a loop and you end up getting None. What type of object is foo? Is it a pure python dict or a django or pymongo wrapper? Show some more please. -

2 Respuestas

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.

Respondido 02 Feb 12, 03:02

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)

o solo mymap.keys() y 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.

Respondido 02 Feb 12, 03:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.