def recurse_encode(obj):
    if isinstance(obj, unicode):
        return obj.encode("UTF-8")
    elif isinstance(obj, dict):
        newdict = {}
        for key,value in obj.iteritems():
            if isinstance(key, unicode):
                key = key.encode("UTF-8")
            value = recurse_encode(value)
            newdict[key] = value
        return newdict
    elif isinstance(obj, (list, tuple)):
        for i, oldobj in enumerate(obj):
            obj[i] = recurse_encode(oldobj)
        return obj
    else:
        return obj
Recursively go through a structure and change unicode strings to byte
dataset/ETHPy150Open bmbouter/Opus/opus/lib/conf/__init__.py/recurse_encode