python格式化dict输出

http://blog.csdn.net/liukeforever/article/details/8982323

python格式化dict输出
如果dict里有unicode or utf-8编码的字符串,缺省是:
In [75]: dd = { 'name': u'功夫熊猫' }
In [76]: dd
Out[76]: {'name': u'\u529f\u592b\u718a\u732b'}

In [77]: dd2 = { 'name': '功夫熊猫' }
In [78]: dd2
Out[78]: {'name': '\xe5\x8a\x9f\xe5\xa4\xab\xe7\x86\x8a\xe7\x8c\xab'}

输出中文不直观,有一种方法是把dict转化成josn格式的字符串输出
print simplejson.dumps(dd, ensure_ascii=False)
这种方法的缺点是不太美观
现在介绍另一种方法

[python]  view plain  copy
  1. #coding=utf-8  
  2.   
  3. import pprint, cStringIO  
  4.   
  5. class UniPrinter(pprint.PrettyPrinter):          
  6.     def format(self, obj, context, maxlevels, level):  
  7.         if isinstance(obj, unicode):  
  8.             out = cStringIO.StringIO()  
  9.             out.write('u"')  
  10.             for c in obj:  
  11.                 if ord(c)<32 or c in u'"\\':  
  12.                     out.write('\\x%.2x' % ord(c))  
  13.                 else:  
  14.                     out.write(c.encode("utf-8"))  
  15.                   
  16.             out.write('"''"') 
  17.             # result, readable, recursive 
  18.             return out.getvalue(), True, False 
  19.         elif isinstance(obj, str): 
  20.             out = cStringIO.StringIO() 
  21.             out.write('"')  
  22.             for c in obj:  
  23.                 if ord(c)<32 or c in '"\\':  
  24.                     out.write('\\x%.2x' % ord(c))  
  25.                 else:  
  26.                     out.write(c)  
  27.                   
  28.             out.write('"')  
  29.             # result, readable, recursive  
  30.             return out.getvalue(), TrueFalse  
  31.         else:  
  32.             return pprint.PrettyPrinter.format(self, obj,  
  33.                 context,  
  34.                 maxlevels,  
  35.                 level)          
  36.   
  37. #UniPrinter().pprint({ u'k"e\\y': u'我爱ä¸*国人' })  
  38. print { 'name': u'功夫熊猫' }  
  39. UniPrinter().pprint({ 'name': u'功夫熊猫' })  
  40. UniPrinter().pprint({'name''\xe5\x8a\x9f\xe5\xa4\xab\xe7\x86\x8a\xe7\x8c\xab'})  
  41. UniPrinter().pprint({'name':'\xe5\x8a\x9f\xe5\xa4\xab\xe7\x86\x8a\xe7\x8c\xab''age':22'address':'yydg.com.cn''male':True})  
如果dict里有unicode or utf-8编码的字符串,缺省是:
In [75]: dd = { 'name': u'功夫熊猫' }
In [76]: dd
Out[76]: {'name': u'\u529f\u592b\u718a\u732b'}

In [77]: dd2 = { 'name': '功夫熊猫' }
In [78]: dd2
Out[78]: {'name': '\xe5\x8a\x9f\xe5\xa4\xab\xe7\x86\x8a\xe7\x8c\xab'}

输出中文不直观,有一种方法是把dict转化成josn格式的字符串输出
print simplejson.dumps(dd, ensure_ascii=False)
这种方法的缺点是不太美观
现在介绍另一种方法

[python]  view plain  copy
  1. #coding=utf-8  
  2.   
  3. import pprint, cStringIO  
  4.   
  5. class UniPrinter(pprint.PrettyPrinter):          
  6.     def format(self, obj, context, maxlevels, level):  
  7.         if isinstance(obj, unicode):  
  8.             out = cStringIO.StringIO()  
  9.             out.write('u"')  
  10.             for c in obj:  
  11.                 if ord(c)<32 or c in u'"\\':  
  12.                     out.write('\\x%.2x' % ord(c))  
  13.                 else:  
  14.                     out.write(c.encode("utf-8"))  
  15.                   
  16.             out.write('"''"') 
  17.             # result, readable, recursive 
  18.             return out.getvalue(), True, False 
  19.         elif isinstance(obj, str): 
  20.             out = cStringIO.StringIO() 
  21.             out.write('"')  
  22.             for c in obj:  
  23.                 if ord(c)<32 or c in '"\\':  
  24.                     out.write('\\x%.2x' % ord(c))  
  25.                 else:  
  26.                     out.write(c)  
  27.                   
  28.             out.write('"')  
  29.             # result, readable, recursive  
  30.             return out.getvalue(), TrueFalse  
  31.         else:  
  32.             return pprint.PrettyPrinter.format(self, obj,  
  33.                 context,  
  34.                 maxlevels,  
  35.                 level)          
  36.   
  37. #UniPrinter().pprint({ u'k"e\\y': u'我爱ä¸*国人' })  
  38. print { 'name': u'功夫熊猫' }  
  39. UniPrinter().pprint({ 'name': u'功夫熊猫' })  
  40. UniPrinter().pprint({'name''\xe5\x8a\x9f\xe5\xa4\xab\xe7\x86\x8a\xe7\x8c\xab'})  
  41. UniPrinter().pprint({'name':'\xe5\x8a\x9f\xe5\xa4\xab\xe7\x86\x8a\xe7\x8c\xab''age':22'address':'yydg.com.cn''male':True})  

猜你喜欢

转载自blog.csdn.net/oMingZi12345678/article/details/51264302