Convert every dictionary value to utf-8 (dictionary comprehension?)

As I had this problem as well, I built a very simple function that allows any dict to be decoded in utf-8 (The problem with the current answer is that it applies only for simple dict).

If it can help anyone, it is great, here is the function :

def utfy_dict(dic):
    if isinstance(dic,unicode):
        return(dic.encode("utf-8"))
    elif isinstance(dic,dict):
        for key in dic:
            dic[key] = utfy_dict(dic[key])
        return(dic)
    elif isinstance(dic,list):
        new_l = []
        for e in dic:
            new_l.append(utfy_dict(e))
        return(new_l)
    else:
        return(dic)

Python 3 version building on that one answer by That1Guy.

{k: str(v).encode("utf-8") for k,v in mydict.items()}

Use a dictionary comprehension. It looks like you're starting with a dictionary so:

 mydict = {k: unicode(v).encode("utf-8") for k,v in mydict.iteritems()}

The example for dictionary comprehensions is near the end of the block in the link.