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学习笔记(五岁以下儿童) 深拷贝-浅拷贝 浅拷贝就是对引用的拷贝(仅仅拷贝父对象) 深拷贝就是对对象的资源拷贝 普通的复制,仅仅是添加了一个指向同一个地址空间的"标签" ...
随机推荐
- (转)linux中项目部署和日志查看
1 查找进程 ps -ef | grep java 查看所有关于java的进程 root 17540 1 0 2009 ? 01:42:27 /usr/java/ ...
- 【SignalR学习系列】4. SignalR广播程序
创建项目 创建一个空的 Web 项目,并在 Nuget 里面添加 SignalR,jQuery UI 包,添加以后项目里包含了 jQuery,jQuery.UI ,和 SignalR 的脚本. 服务端 ...
- 【CSS】整屏大背景
1. 利用div的层次,设置底层div充满屏幕,并给div设置背景图 <div id="Layer1" style="position:absolute;top:0 ...
- HDU-4787 GRE Words Revenge 解题报告
这是我之前博客里提到的一道AC自动机的练手题,但是要完成这道题,我之前博客里提到的东西还不够,这里总结一下这道题. 这道题不是一般的裸的AC自动机,它的询问和插入是交叉出现的所以用我之前写的板子不大合 ...
- 内核对象kobject和sysfs(2)——kref分析
内核对象kobject和sysfs(2)--kref分析 在介绍ref之前,先贴上kref的结构: struct kref { atomic_t refcount; }; 可以看到,kref只是包含一 ...
- Chrome调试工具developer tool技巧
Chrome这个浏览器赞的不能再赞了,给前端的开发调试工作带来了极大的效率提升. Chrome的简洁.快速吸引了无数人,它的启动速度.页面解析速度都很快,同时得益于Google V8的快速,Javas ...
- C++ 之 Asio 库
1 简介 Asio 是一个跨平台的 C++ 库,常用于网络编程.底层的 I/O 编程等 (low-level I/O),其结构框架如下: 2 使用 Asio 2.1 下载 Asio 可分为 ...
- R语言 write.xlsx() 写入同一excel,及同一sheet注意
write.xlsx(x, file, sheetName="Sheet1", col.names=TRUE, row.names=TRUE, append=FALSE, show ...
- 9. leetcode 389. Find the Difference
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- 第二棵树:Splay
Splay这东西神难打--什么都没动板子敲上就直逼200行了,而且非常难记(仿佛是模板长的必然结果).但是为什么还要学呢?据说是因为它可以实现区间操作.但是自从我得知无旋Treap也能做到这些,默默对 ...