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学习笔记(五岁以下儿童) 深拷贝-浅拷贝 浅拷贝就是对引用的拷贝(仅仅拷贝父对象) 深拷贝就是对对象的资源拷贝 普通的复制,仅仅是添加了一个指向同一个地址空间的"标签" ...
随机推荐
- VSCode自定义配色方案
说明 本文更新于2017-07-24,使用VSCode 1.14.1,操作系统为Windows. 配置文件 "文件-首选项-颜色主题"即可显示所有可用的颜色主题,上下选择后Ente ...
- (转)eclipse安装jetty
背景:在项目开发的过程中,一个老的项目使用的是jetty启动,在用tomcat启动的过程中出现了启动不了的异常,浪费了好多时间.因为项目一直是用jetty启动的,为了不浪费时间,也只好改变思路选择je ...
- (转)流量加速插件 FinalSpeed介绍及一键安装教程
原文章连接:https://blog.kuoruan.com/82.html 1 介绍 官方介绍:FinalSpeed是高速双边加速软件,可加速所有基于tcp协议的网络服务,在高丢包和高延迟环境下,仍 ...
- 第一章之s5pv210启动顺序
我所使用的开发板是:友善之臂smart210,cpu为s5pv210.u-boot版本是:u-boot-2012-10 1,首先在u-boot中配置相对应的开发板的配置文件 #make s5p_gon ...
- vue组件的那些事($children,$refs,$parent)的使用
如果项目很大,组件很多,怎么样才能准确的.快速的寻找到我们想要的组件了?? 1)$refs 首先你的给子组件做标记.demo :<firstchild ref="one"&g ...
- Codeforces Round #427 (Div. 2)
B. The number on the board 题意: 有一个数字,它的每个数位上的数字的和不小于等于k.现在他改变了若干位,变成了一个新的数n,问现在的数和原来的数最多有多少位不同. 思路: ...
- MongoDB增 删 改 查
增 增加单篇文档 > db.stu.insert({sn:'001', name:'lisi'}) WriteResult({ "nInserted" : 1 }) > ...
- 15. leetcode 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...
- (1)xcode基本设置和控制器等介绍
1.在IOS应用程序中,如果没有对storyBoard进和设置它的界面是非常大,有时候如果把元素放在右边会出现运行程序时超出显示界面而不显示的问题.为了解决这个问题我们通常会在用模拟器设置调试界面的时 ...
- mongodb分片部署
Mongodb 分片部署 配置mongodb集群,比如 在3个server上配置 3 shard的Mongodb集群: 架构: 1.每片数据需要3个mongod server,2个为主从数据节点:1个 ...