首页 > 编程语言 > 详细

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

时间:2014-10-16 21:04:36      阅读:320      评论:0      收藏:0      [点我收藏+]
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)


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

原文:http://my.oschina.net/cppblog/blog/333017

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!