python格式化dict输出
2016-04-27 20:41
597 查看
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
#coding=utf-8
import pprint, cStringIO
class UniPrinter(pprint.PrettyPrinter):
def format(self, obj, context, maxlevels, level):
if isinstance(obj, unicode):
out = cStringIO.StringIO()
out.write('u"')
for c in obj:
if ord(c)<32 or c in u'"\\':
out.write('\\x%.2x' % ord(c))
else:
out.write(c.encode("utf-8"))
out.write('"''"')
# result, readable, recursive
return out.getvalue(), True, False
elif isinstance(obj, str):
out = cStringIO.StringIO()
out.write('"')
for c in obj:
if ord(c)<32 or c in '"\\':
out.write('\\x%.2x' % ord(c))
else:
out.write(c)
out.write('"')
# result, readable, recursive
return out.getvalue(), True, False
else:
return pprint.PrettyPrinter.format(self, obj,
context,
maxlevels,
level)
#UniPrinter().pprint({ u'k"e\\y': u'我爱ä¸*国人' })
print { 'name': u'功夫熊猫' }
UniPrinter().pprint({ 'name': u'功夫熊猫' })
UniPrinter().pprint({'name': '\xe5\x8a\x9f\xe5\xa4\xab\xe7\x86\x8a\xe7\x8c\xab'})
UniPrinter().pprint({'name':'\xe5\x8a\x9f\xe5\xa4\xab\xe7\x86\x8a\xe7\x8c\xab', 'age':22, 'address':'yydg.com.cn', 'male':True})
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
#coding=utf-8
import pprint, cStringIO
class UniPrinter(pprint.PrettyPrinter):
def format(self, obj, context, maxlevels, level):
if isinstance(obj, unicode):
out = cStringIO.StringIO()
out.write('u"')
for c in obj:
if ord(c)<32 or c in u'"\\':
out.write('\\x%.2x' % ord(c))
else:
out.write(c.encode("utf-8"))
out.write('"''"')
# result, readable, recursive
return out.getvalue(), True, False
elif isinstance(obj, str):
out = cStringIO.StringIO()
out.write('"')
for c in obj:
if ord(c)<32 or c in '"\\':
out.write('\\x%.2x' % ord(c))
else:
out.write(c)
out.write('"')
# result, readable, recursive
return out.getvalue(), True, False
else:
return pprint.PrettyPrinter.format(self, obj,
context,
maxlevels,
level)
#UniPrinter().pprint({ u'k"e\\y': u'我爱ä¸*国人' })
print { 'name': u'功夫熊猫' }
UniPrinter().pprint({ 'name': u'功夫熊猫' })
UniPrinter().pprint({'name': '\xe5\x8a\x9f\xe5\xa4\xab\xe7\x86\x8a\xe7\x8c\xab'})
UniPrinter().pprint({'name':'\xe5\x8a\x9f\xe5\xa4\xab\xe7\x86\x8a\xe7\x8c\xab', 'age':22, 'address':'yydg.com.cn', 'male':True})
相关文章推荐
- 算法竞赛入门经典第三章总结(python实现)
- Learn python the hard way. python test program 2016.04.27
- 『 Spark 』2. spark 基本概念解析
- python文件操作命令
- 二分k均值 Python实现
- Python数据分析学习笔记六
- 如何使用PyCharm编写Spark程序(pyspark)
- Python中if-else语句的多种写法
- python+selenium 简单尝试
- Python正则表达式汇总
- windows下安装gulp-sass编译不过去报错的解决方案;
- Python Web后端处理业务
- Notepadpp的Python Script插件
- Ant、Gradle、Python三种打包方式的介绍
- 常用的 Python 爬虫技巧总结
- Python logging模块详解
- Python开发的10个小贴士
- Python数据分析学习笔记五
- Python 主要模块和常用方法简览
- python常用50个模块