码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-10-16 22:30:04      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:os   ar   for   sp   数据   on   ef   bs   line   

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下正常输出

标签:os   ar   for   sp   数据   on   ef   bs   line   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!