python formatter模块
formatter 模块
formatter 模块提供了一些可用于 htmllib 的格式类( formatter classes ).
这些类有两种, formatter 和 writer . formatter 将 HTML 解析器的标签和数据流转换为适合输出设备的事件流( event stream ), 而 writer 将事件流输出到设备上.
大多情况下, 你可以使用 AbstractFormatter 类进行格式化. 它会根据不同的格式化事件调用 writer 对象的方法. AbstractWriter 类在每次方法调用时打印一条信息.
使用 formatter 模块将 HTML 转换为事件流
!/usr/bin/python
import formatter
import htmllib
w=formatter.AbstractWriter() #writer将事件流输出到设备上
f=formatter.AbstractFormatter(w) #formatter将HTML解析器的标签和数据流转换为适合输出的事件流
file=open("index.html")
p=htmllib.HTMLParser(f)
p.feed(file.read())
p.close()
file.close()
运行结果:
windyang@windyang-desktop:~/python$ python formatter-example-1.py
send_flowing_data('\xa0')
send_line_break()
send_paragraph(1)
new_font(('h1', 0, 1, 0))
send_flowing_data('Dive Into Python')
send_line_break()
send_paragraph(1)
new_font(None)
send_flowing_data('Python from novice to pro')
send_line_break()
send_paragraph(1)
send_flowing_data('Find:')
send_flowing_data('\xa0')
send_line_break()
send_paragraph(1)
new_font(('h2', 0, 1, 0))
new_font(None)
new_font((None, 1, None, None))
send_flowing_data('Dive Into')
send_flowing_data(' Python')
new_font(None)
send_flowing_data(' is a free')
send_flowing_data(' Python')
send_flowing_data(' book for experienced programmers. You can')
send_flowing_data(' read the book')
send_flowing_data('[1]')
send_flowing_data(' online, or')
send_flowing_data(' download it')
send_flowing_data('[2]')
send_flowing_data(' in a variety of formats. It is also available in')
send_flowing_data(' multiple languages')
send_flowing_data('[3]')
send_flowing_data('.')
send_line_break()
send_paragraph(1)
new_font(('h2', 0, 1, 0))
send_flowing_data('Read')
send_flowing_data(' ')
new_font(('h2', 1, 1, 0))
send_flowing_data('Dive Into')
send_flowing_data(' Python')
new_fo
相关文档:
原文地址 http://www.javaeye.com/wiki/Python/1371-python-graphics-library-pil-python-image-library-introduction
关于PIL库的一些概念
pil能处理的图片
类型
pil可以处理光栅图片(像素数据组成的的块)。
通道
一个图片可以包含一到多个数据通道,如果这些通道具有相同的维数和深度,Pil允许将这些通道进行叠加 ......
Python 序列
列表、元组和字符串都是序列,但是序列是什么,它们为什么如此特别呢?序列的两个主要特点是索引操作符和切片操作符。索引操作符让我们可以从序列中抓取一个特定项目。切片操作符让我们能够获取序列的一个切片,即一部分序列。
使用序列
例9.5 使用序列
#!/usr/bin/python
# Filename: seq.py
shoplist ......
不多说了,直接看代码吧!
import os
path = 'e:/Download/'
kzm = []
kzmTemp = set()
kzmTemp2 = []
dict = {}
for root,dirs,files in os.walk(path):
for file in files:
ext = os.path.splitext(file)[1][1:]
  ......
Python 用下划线作为变量前缀和后缀指定特殊变量。
_xxx
不能用'from module import *'导入
__xxx__ 系统定义名字
__xxx
类中的私有变量名
核心风格:避免用下划线作为变量名的开始。
因为下划线对解释器有特殊的意义,而且是内建标识符所使用的符号,我们建议程序 ......