-1

I am using pymongo to insert a complex structure as a row in a collection. The structure is a dict of list of dicts of lists of dicts etc..

Is there a way to find which field is unicode instead of str, that causes the error? I have tried:

def dump(obj):
  with open('log', 'w') as flog:
    for attr in dir(obj):
      t, att = type(attr), getattr(obj, attr)
      output =  "obj.%s = %s" % (t, att)
      flog.write(output)

but no luck so far.

Any clever recursive way to print everything maybe?

Thanks

stelios
  • 2,679
  • 5
  • 31
  • 41

1 Answers1

0

The following helped me to find out which dict contained unicode values, since a dict can be identified by its keys. The list-case doesn't help.

def find_the_damn_unicode(obj):

    if isinstance(obj, unicode):
        ''' The following conversion probably doesn't do anything meaningfull since
            obj is probably a primitive type, thus passed by value. Thats why encoding
            is also performed inside the for loops below'''
        obj = obj.encode('utf-8')
        return obj

    if isinstance(obj, dict):
        for k, v in obj.items():
            if isinstance(v, unicode):
                print 'UNICODE value with key ', k
                obj[k] = obj[k].encode('utf-8')
            else:
                obj[k] = find_the_damn_unicode(v)

    if isinstance(obj, list):
        for i, v in enumerate(obj):
            if isinstance(v, unicode):
                print 'UNICODE inside a ... list'
                obj[i] = obj[i].encode('utf-8')
            else:
                obj[i] = find_the_damn_unicode(v)

    return obj
stelios
  • 2,679
  • 5
  • 31
  • 41