Python数据结构中包含中文时在Windows下正常输出

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)


你可能感兴趣的:(windows,Win32,unicode,python,编码,console,乱码,gbk,print)