Python:Can';在mongo插入期间找不到导致bson.errors.InvalidDocument的unicode字段
我使用pymongo在集合中插入一个复杂的结构作为一行。该结构是一个dict of list of dict of list of dict等 有没有办法找到导致错误的字段是unicode而不是str?我试过:Python:Can';在mongo插入期间找不到导致bson.errors.InvalidDocument的unicode字段,python,mongodb,character-encoding,pymongo,script-debugging,Python,Mongodb,Character Encoding,Pymongo,Script Debugging,我使用pymongo在集合中插入一个复杂的结构作为一行。该结构是一个dict of list of dict of list of dict等 有没有办法找到导致错误的字段是unicode而不是str?我试过: def dump(obj): with open('log', 'w') as flog: for attr in dir(obj): t, att = type(attr), getattr(obj, attr) output = "obj.%s
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)
但到目前为止运气不好
有没有什么聪明的递归方法可以打印所有内容
感谢以下内容帮助我找到了包含unicode值的dict,因为dict可以通过其键来识别。列表案例没有帮助
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