- Python-文本处理
- Python-文本处理简介
- Python - 文本处理环境
- Python - 字符串不变性
- Python - 排序行
- Python - 重新格式化段落
- Python - 计算段落中的标记
- Python - 二进制 ASCII 转换
- Python - 字符串作为文件
- Python-向后读取文件
- Python - 过滤重复单词
- Python - 从文本中提取电子邮件
- Python - 从文本中提取 URL
- Python - 漂亮的打印
- Python - 文本处理状态机
- Python - 大写和翻译
- Python - 标记化
- Python - 删除停用词
- Python - 同义词和反义词
- Python - 文本翻译
- Python-单词替换
- Python-拼写检查
- Python - WordNet 接口
- Python - 语料库访问
- Python - 标记单词
- Python - 块和缝隙
- Python - 块分类
- Python-文本分类
- Python-二元组
- Python - 处理 PDF
- Python-处理Word文档
- Python - 读取 RSS 提要
- Python-情感分析
- Python - 搜索和匹配
- Python - 文本修改
- Python-文本换行
- Python-频率分布
- Python-文本摘要
- Python - 词干算法
- Python - 约束搜索
Python - 漂亮的打印数字
python 模块pprint用于为 python 中的各种数据对象提供正确的打印格式。这些数据对象可以表示字典数据类型,甚至可以表示包含 JSON 数据的数据对象。在下面的示例中,我们将看到应用 pprint 模块之前和应用之后数据的外观。
import pprint
student_dict = {'Name': 'Tusar', 'Class': 'XII',
'Address': {'FLAT ':1308, 'BLOCK ':'A', 'LANE ':2, 'CITY ': 'HYD'}}
print student_dict
print "\n"
print "***With Pretty Print***"
print "-----------------------"
pprint.pprint(student_dict,width=-1)
当我们运行上面的程序时,我们得到以下输出 -
{'Address': {'FLAT ': 1308, 'LANE ': 2, 'CITY ': 'HYD', 'BLOCK ': 'A'}, 'Name': 'Tusar', 'Class': 'XII'}
***With Pretty Print***
-----------------------
{'Address': {'BLOCK ': 'A',
'CITY ': 'HYD',
'FLAT ': 1308,
'LANE ': 2},
'Class': 'XII',
'Name': 'Tusar'}
处理 JSON 数据
Pprint 还可以通过将 JSON 数据格式化为更易读的格式来处理它们。
import pprint
emp = {"Name":["Rick","Dan","Michelle","Ryan","Gary","Nina","Simon","Guru" ],
"Salary":["623.3","515.2","611","729","843.25","578","632.8","722.5" ],
"StartDate":[ "1/1/2012","9/23/2013","11/15/2014","5/11/2014","3/27/2015","5/21/2013",
"7/30/2013","6/17/2014"],
"Dept":[ "IT","Operations","IT","HR","Finance","IT","Operations","Finance"] }
x= pprint.pformat(emp, indent=2)
print x
当我们运行上面的程序时,我们得到以下输出 -
{ 'Dept': [ 'IT',
'Operations',
'IT',
'HR',
'Finance',
'IT',
'Operations',
'Finance'],
'Name': ['Rick', 'Dan', 'Michelle', 'Ryan', 'Gary', 'Nina', 'Simon', 'Guru'],
'Salary': [ '623.3',
'515.2',
'611',
'729',
'843.25',
'578',
'632.8',
'722.5'],
'StartDate': [ '1/1/2012',
'9/23/2013',
'11/15/2014',
'5/11/2014',
'3/27/2015',
'5/21/2013',
'7/30/2013',
'6/17/2014']}