Python学习笔记5
1.关于global声明变量的错误例子
- I ran across this warning:
- #!/usr/bin/env python2.3
- VAR = 'xxx'
- if __name__ == '__main__':
- global VAR
- VAR = 'yyy'
- ---
- OUTPUT:
- ./var.py:0: SyntaxWarning: name 'VAR' is assigned to before global declaration
- ----
- But, a little twiddle quiets the warning, and I have no idea why:
- #!/usr/bin/env python2.3
- VAR = 'xxx'
- def set_var():
- global VAR
- VAR = 'yyy'
- if __name__ == '__main__':
- set_var()
- ---
- No output.
- Global is normally used within a function definition to allow it to assign
- to names defined outside the function (as in your 2nd example). In your
- first example global is outside any function definition, and therefore not
- meaningful, as well as giving a SyntaxWarning.
2.HTMLParser中feed
HTMLParser的feed()方法会调用
handle_starttag(), handle_data(), handle_endtag()方法
- #! /usr/bin/env python
- #coding=utf-8
- from htmlentitydefs import entitydefs
- from HTMLParser import HTMLParser
- import sys
- class TitleParser(HTMLParser):
- def __init__(self):
- self.title = ' '
- self.readingtitle = 0
- HTMLParser.__init__(self)
- def handle_starttag(self, tag, attrs):
- if tag == 'title':
- self.readingtitle = 1
- def handle_data(self, data):
- if self.readingtitle:
- self.title += data
- def handle_endtag(self, tag):
- if tag == 'title':
- self.readingtitle = 0
- def handle_entityref(self, name):
- if entitydefs.has_key(name):
- self.handle_data(entitydefs[name])
- else:
- self.handle_data('&' + name + ';')
- def gettitle(self):
- return self.title
- fd = open(sys.argv[1])
- tp = TitleParser()
- tp.feed(fd.read())
- print "Title is:", tp.gettitle()
3 HTMLParser
HTMLParser是python用来解析html的模块。它可以分析出html里面的标签、数据等等,是一种处理html的简便途径。 HTMLParser采用的是一种事件驱动的模式,当TMLParser找到一个特定的标记时,它会去调用一个用户定义的函数,以此来通知程序处理。它 主要的用户回调函数的命名都是以handler_开头的,都是HTMLParser的成员函数。当我们使用时,就从HTMLParser派生出新的类,然 后重新定义这几个以handler_开头的函数即可。
handle_startendtag 处理开始标签和结束标签
handle_starttag 处理开始标签,比如<xx>
handle_endtag 处理结束标签,比如</xx>
handle_charref 处理特殊字符串,就是以&#开头的,一般是内码表示的字符
handle_entityref 处理一些特殊字符,以&开头的,比如
handle_data 处理数据,就是<xx>data</xx>中间的那些数据
handle_comment 处理注释
handle_decl 处理<!开头的,比如<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
handle_pi 处理形如<?instruction>的东西
[python] view plaincopyprint?
>>> help(HTMLParser.HTMLParser.handle_endtag)
Help on method handle_endtag in module HTMLParser:
handle_endtag(self, tag) unbound HTMLParser.HTMLParser method
# Overridable -- handle end tag
>>> help(HTMLParser.HTMLParser.handle_data)
Help on method handle_data in module HTMLParser:
handle_data(self, data) unbound HTMLParser.HTMLParser method
# Overridable -- handle data
>>> help(HTMLParser.HTMLParser.handle_charref)
Help on method handle_charref in module HTMLParser:
handle_charref(self, name) unbound HTMLParser.HTMLParser method
# Overridable -- handle character reference
>>> help(HTMLParser.HTMLParser.handle_decl)
Help on method handle_decl in module HTMLParser:
handle_decl(self, decl) unbound HTMLParser.HTMLParser method
# Overridable -- handle declaration
>>> help(HTMLParser.HTMLParser.handle_startendtag)
Help on method handle_startendtag in module HTMLParser:
handle_startendtag(self, tag, attrs) unbound HTMLParser.HTMLParser method
# Overridable -- finish processing of start+end tag: <tag.../>
>>> help(HTMLParser.HTMLParser.handle_endtag)
Help on method handle_endtag in module HTMLParser:
handle_endtag(self, tag) unbound HTMLParser.HTMLParser method
# Overridable -- handle end tag
>>> help(HTMLParser.HTMLParser.handle_data)
Help on method handle_data in module HTMLParser:
handle_data(self, data) unbound HTMLParser.HTMLParser method
# Overridable -- handle data
>>> help(HTMLParser.HTMLParser.handle_charref)
Help on method handle_charref in module HTMLParser:
handle_charref(self, name) unbound HTMLParser.HTMLParser method
# Overridable -- handle character reference
>>> help(HTMLParser.HTMLParser.handle_decl)
Help on method handle_decl in module HTMLParser:
handle_decl(self, decl) unbound HTMLParser.HTMLParser method
# Overridable -- handle declaration
>>> help(HTMLParser.HTMLParser.handle_startendtag)
Help on method handle_startendtag in module HTMLParser:
handle_startendtag(self, tag, attrs) unbound HTMLParser.HTMLParser method
# Overridable -- finish processing of start+end tag: <tag.../>
4. re.findall()
使用findall搜索得到的匹配结果,返回值是一个表,另在正则表达式中,使用‘()’可以设置返回结果为选中的内容。
5. 用python读写excel文件数据
import csv模块,将xls格式文件,重新save as为csv格式,具体使用如下
- #!/usr/bin/env python
- # -*- coding:utf-8 -*-
- import csv
- with open('egg2.csv', 'wb') as csvfile:
- spamwriter = csv.writer(csvfile, delimiter=' ',quotechar='|', quoting=csv.QUOTE_MINIMAL)
- spamwriter.writerow(['a', '', '', '', ''])
- spamwriter.writerow(['b', '', '', '', ''])
- spamwriter.writerow(['c', '', '', '', ''])
- spamwriter.writerow(['d', '','','', ''])
- spamwriter.writerow(['e', '','','', ''])
or
- #!/usr/bin/env python
- # -*- coding:utf-8 -*-
- import csv
- with open('egg2.csv', 'wb') as csvfile:
- spamwriter = csv.writer(csvfile,dialect='excel')
- spamwriter.writerow(['a', '', '', '', ''])
- spamwriter.writerow(['b', '', '', '', ''])
- spamwriter.writerow(['c', '', '', '', ''])
- spamwriter.writerow(['d', '','','', ''])
- spamwriter.writerow(['e', '','','', ''])
第一种为所有数据存放到excel中一列,而第二种为数据分5列存入
5.正则表达式中.*?
在正则表达式中使用.*?匹配字符时,要注意其不包含\n,当中间含有换行时,可使用(.|\n)*?进行匹配
Python学习笔记5的更多相关文章
- python学习笔记整理——字典
python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...
- VS2013中Python学习笔记[Django Web的第一个网页]
前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...
- python学习笔记之module && package
个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...
- python学习笔记(六)文件夹遍历,异常处理
python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...
- python学习笔记--Django入门四 管理站点--二
接上一节 python学习笔记--Django入门四 管理站点 设置字段可选 编辑Book模块在email字段上加上blank=True,指定email字段为可选,代码如下: class Autho ...
- python学习笔记--Django入门0 安装dangjo
经过这几天的折腾,经历了Django的各种报错,翻译的内容虽然不错,但是与实际的版本有差别,会出现各种奇葩的错误.现在终于找到了解决方法:查看英文原版内容:http://djangobook.com/ ...
- python学习笔记(一)元组,序列,字典
python学习笔记(一)元组,序列,字典
- Pythoner | 你像从前一样的Python学习笔记
Pythoner | 你像从前一样的Python学习笔记 Pythoner
- OpenCV之Python学习笔记
OpenCV之Python学习笔记 直都在用Python+OpenCV做一些算法的原型.本来想留下发布一些文章的,可是整理一下就有点无奈了,都是写零散不成系统的小片段.现在看 到一本国外的新书< ...
- python学习笔记(五岁以下儿童)深深浅浅的副本复印件,文件和文件夹
python学习笔记(五岁以下儿童) 深拷贝-浅拷贝 浅拷贝就是对引用的拷贝(仅仅拷贝父对象) 深拷贝就是对对象的资源拷贝 普通的复制,仅仅是添加了一个指向同一个地址空间的"标签" ...
随机推荐
- 什么是B-Tree
B-Tree就是我们常说的B树,一定不要读成B减树,否则就很丢人了.B树这种数据结构常常用于实现数据库索引,因为它的查找效率比较高. B-Tree与二叉查找树的对比 我们知道二叉查找树查询的时间复杂度 ...
- mongodb中limit与skip方法
Mongodb Limit()方法 如果需要在mongodb中获取指定数量的数据记录,这时候就要用到limit()方法,该方法需要接收一个数字参数 基本语法: DB.COLLECTION_NAME. ...
- 连接池报错 Proxool Provider unable to load JAXP configurator file: proxool.xml
上篇博文讲到简易配置 proxool 连接池:http://www.cnblogs.com/linnuo/p/7232380.html 由于把说明注释留在了 proxool.xml 配置文件里导致配置 ...
- DDD理论学习系列(13)-- 模块
DDD理论学习系列--案例及目录 1. 引言 Module,即模块,是指提供特定功能的相对独立的单元.提到模块,你肯定就会想到模块化设计思想,也就是功能的分解和组合.对于简单问题,可以直接构建单一模块 ...
- Hbase 基础 - shell 与 客户端
版权说明: 本文章版权归本人及博客园共同所有,转载请标明原文出处(http://www.cnblogs.com/mikevictor07/),以下内容为个人理解,仅供参考. 一.简介 Hbase是在 ...
- 【整理】01. Fiddler 杂记
抓手机包步骤: Tools -- Fiddler Options -- Connections (默认)Fiddler listens on port:8888 (勾选)Allow remote co ...
- Codeforces 828B Black Square(简单题)
Codeforces 828B Black Square(简单题) Description Polycarp has a checkered sheet of paper of size n × m. ...
- NET中解决KafKa多线程发送多主题的问题
一般在KafKa消费程序中消费可以设置多个主题,那在同一程序中需要向KafKa发送不同主题的消息,如异常需要发到异常主题,正常的发送到正常的主题,这时候就需要实例化多个主题,然后逐个发送. 在NET中 ...
- vue指令v-html示例解析
更新元素的innerHTML,不会作为vue模板编译,可用组件来代替. 在网站上动态渲染任意 HTML 是非常危险的,因为容易导致 xss攻击.只在可信内容上使用 v-html,永不用在用户提交的内容 ...
- 数据结构二叉树的所有基本功能实现。(C++版)
本人刚学数据结构,对树的基本功能网上找不到C++代码 便自己写了一份,贴出方便大家进行测试和学习. 大部分功能未测试,如有错误或者BUG,请高手们指教一下,谢谢. 结点声明: BinTreeNode. ...