python基础教程笔记-项目1-即时标记-Day4

今天主要看下re.sub函数和Handler类的sub函数

先看下re.sub函数

re.sub共有5个参数,不过一般写前三个就好了,即pattern,repl和string

pattern表示正则中的模式字符串,repl可以是字符串,也可以是函数。string为要进行替换的字符串

先看一段代码:

import re

def Num2A(match):
	return 'A'

a = re.sub(r'(\d+)', Num2A, 'he123he')
print a
a = re.sub(r'(\d+)', r'A', 'he123he')
print a

执行结果:

代码分别通过函数Num2A和字符串A,将字符串hehe123hehe转化为heAhe。

当re.sub的第二个参数repl为函数时,函数的返回值就是想替换的结果。

再看下Handler的sub函数

看下面一段代码

import re

class Handler:
	def sub_emphasis(self, match):
		return '<em>%s</em>' % match.group(1)
	def callback(self, prefix, name, *args):
		method = getattr(self, prefix+name, None)
		if callable(method): return method(*args)
	def sub(self, name):
		def substitution(match):
			result = self.callback('sub_', name, match)
			if result is None:
result = match.group(0)
			return result
		return substitution

a = Handler()
a = re.sub(r'\*(.+?)\*', a.sub('emphasis'), r'This *is* a test')
print a

执行结果为:

可以看到sub返回了一个新函数,而这个函数会被当成re.sub中的替换函数来使用。

这里书上写错了,应该是

if result is None: result = match.group(0)

不是if result isNone:match.group(0)

因为如果在对象中没找到匹配的函数,result会为None,那么为了不对string做任何修改,应只是返回原来的方法,即return match.group(0)

时间: 2024-08-29 04:24:37

python基础教程笔记-项目1-即时标记-Day4的相关文章

python基础教程笔记-项目1-即时标记-Day3

昨天实现了简单的txt转html,今天更深入一步. 主要了解下带星号的参数.getattr函数和callable函数 先看Handler类: class Handler: def callback(self, prefix, name, *args): method = getattr(self, prefix+name, None) if callable(method): return method(*args) def start(self, name): self.callback('s

python基础教程笔记-项目1-即时标记-Day2

昨天主要了解了下生成器,用文档测下lines: def lines(file): for line in file: yield line yield '\n' for i in lines(sys.stdin): if i: print i print '---' 测试文档test_input.txt: hello how are you how do you do fine 执行: 输出结果test_output.txt: hello --- --- how are you --- how

python基础教程笔记-项目2-画幅好画-Day1

今天开始看项目二:画幅好画 项目中会用到图形生成包ReportLab,因此首先装好这个包:easy_install reportlab 从书中可以了解到,这个包主要用于作图. 比如说写字.画线等.执行代码: from reportlab.lib import colors from reportlab.graphics.shapes import Drawing,String,PolyLine from reportlab.graphics import renderPDF d = Drawin

python基础教程笔记-项目2-画幅好画-Day2

本项目剩下部分主要使用量urllib和LinePlot. 什么是urllib? urllib模块提供的上层接口,使我们可以像读取本地文件一样读取www和ftp上的数据. 看代码: import urllib print urllib.urlopen('http://www.baidu.com').read() 执行效果: 重点需要注意的是urlopen函数: 关于urlopen的一些使用实例可参考链接: http://blog.csdn.net/leer168/article/details/8

python基础教程总结15——1.即时标记

1. 测试文档: # test_input.txt Welcome to World Wide Spam. Inc. These are the corporate web pages of *World Wide Spam*, Inc. We hope you find your stay enjoyable, and that you will sample many of our products. A short history of the company World Wide Spa

Python基础教程__项目(公告板)

由于最近学习Python,从最基础的Python基础教程学起,其中最后的十个项目还是很不错的.个人认为.本人新手,如有错误,还请指教. 书上用的是PostgreSQL,这里用的是MySQL.由于这是一个CGI项目.所以事先需要准备一个可以运行CGI脚本的试验环境. 本次用的是Apache运行cgi.配置网上很多. 其次需要创建一个数据表: CREATE TABLE `messages` (   `id` int(11) NOT NULL AUTO_INCREMENT,   `subject` v

python基础教程笔记——画幅好画(详解)

今天写一下基础教程里面的第二个项目,主要使用python来做一个pdf的图,比较简单. 首先我们需要安装用到的模块pip install reportlab即可. 书上是用urlopen从往上下了一个txt文件,然后打开处理一下得到数据,因为我从这个url路径没有获取到数据,所以直接写了一点数据在程序里. urlopen比较简单,以后写爬虫也会经常用到,所以这里就不讲了,只单独讲讲reportlab模块. #encoding:utf8 from reportlab.graphics.shapes

python 基础教程 笔记 一

第一章 python 基础知识 1.1 数字和数学表达式 1.2 python 2.x 和python 3.x print的区别 1.3 python 2.x 和 python 3.x input 的区别 1.4 数学函数 1.5 input 和 raw_input 区别 第二章 列表和元组 第三章 使用字符串 1.1 数字和表达式 Python 默认的除法:整数/整数,无论是否整除都得到整数,不整除的截取小时部分 1 / 2 0 如果想要Python 执行普通的除法,可以之用浮点数来实现也可以

Python基础教程笔记——使用字符串

使用字符串 Table of Contents 1 基本字符串操作 2 字符串格式化:精简版 2.1 用字符串格式化操作符 2.2 用string的Template格式化字符串 3 字符串格式化:完整版 3.1 转换说明符 3.2 简单转换 3.3 字段宽度和精度 3.4 符号,对齐和 0 填充 4 字符串方法 4.1 find 4.2 join 4.3 lower 4.4 replace 4.5 split 4.6 strip 4.7 translate 1 基本字符串操作 说明:字符串也是序