def obj2str( obj,with_endline= True ): ‘‘‘ 为了使包含中文的数据结构能够在win32下面正常打印出字符串,将所有的字符串转换为unicode。 ‘‘‘ new_str =u‘‘ if ‘nt‘ == os.name: if hasattr(obj, "__dict__") : new_str+= "{" for kev,value in obj.iteritems(): new_str += obj2str(kev) + ":" + obj2str(value) + "," if(len(obj)): new_str= new_str[:-1] +"}" else: new_str +="}" elif hasattr(obj,"__iter__"): new_str += "[" for item in obj: new_str += obj2str(item) + "," if(len(obj)): new_str= new_str[:-1] +"]" else: new_str +="]" elif type(obj)==str: new_str += ‘u"‘ + obj.decode(‘utf-8‘) +‘"‘ else: new_str += str( obj) return str(new_str)
时间: 2025-01-04 03:54:33