您的位置:首页 > 编程语言 > Python开发

Python学习笔记5

2015-07-10 16:25 701 查看

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', '1', '1', '2', '2'])

spamwriter.writerow(['b', '3', '3', '6', '4'])

spamwriter.writerow(['c', '7', '7', '10', '4'])

spamwriter.writerow(['d', '11','11','11', '1'])

spamwriter.writerow(['e', '12','12','14', '3'])


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', '1', '1', '2', '2'])
spamwriter.writerow(['b', '3', '3', '6', '4'])
spamwriter.writerow(['c', '7', '7', '10', '4'])
spamwriter.writerow(['d', '11','11','11', '1'])
spamwriter.writerow(['e', '12','12','14', '3'])


第一种为所有数据存放到excel中一列,而第二种为数据分5列存入

5.正则表达式中.*?

在正则表达式中使用.*?匹配字符时,要注意其不包含\n,当中间含有换行时,可使用(.|\n)*?进行匹配
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: