注:文章原文为Dr. Charles Severance 的 《Python for Informatics》。文中代码用3.4版改写,并在本机测试通过。

11.7 调试

  Python有一些简单和基本的内置文档,当你想快速复习触发你记忆的特定方法,这将非常有用。这个文档可以通过Python解释器在互动模式下查看。

  你可以使用help()命令带出互动的帮助系统

>>> help()

Welcome to Python 3.4's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/3.4/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics". Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

help> modules

  帮助系统下键入modules命令,帮助系统将显示所有可用的模块。

  如果你知道你想使用的模块名,你可以使用dir()命令显示模块中的方法(注意要退出帮助系统)

>>>import re

>>>dir(re)

['A', 'ASCII', 'DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', '_alphanum_bytes', '_alphanum_str', '_cache', '_cache_repl', '_compile', '_compile_repl', '_expand', '_pattern_type', '_pickle', '_subx', 'compile', 'copyreg', 'error', 'escape', 'findall', 'finditer', 'fullmatch', 'match', 'purge', 'search', 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'sys', 'template']

  你同样可以用help命令得到关于特定方法的少量文档

>>> help (re.search)
Help on function search in module re:
search(pattern, string, flags=0)
Scan through string looking for a match to the pattern, returning
a match object, or None if no match was found.
>>>
  这个内置的文档不是非常广泛,但当你不想访问网页或搜索引擎而快速获取帮助时,它将非常有用。

11.8 词汇表

  脆弱代码(brittle code):在输入数据是一种特殊格式时可以运行,但是当输入的数据和正确格式有一些差异时容易失效的代码。

  贪婪匹配(greedy matching):正则表达式中"+"和"*"匹配扩展到最大可能的字符串。

  grep: 在绝大多数Unix系统中可用的命令,用来搜索整个文本文件,找出匹配正则表达式的行。它的名字代表通用正则表达式分析器。

  正则表达式:一个表达更加复杂查询字符串的语言。可能包含特殊字符表示只匹配查找位于行的开头或结尾,以及其他相似功能。

  通配符:可以匹配任意字符的特殊字符。在正则表达式中这个通配符是"."。
11.9 练习

练习11.1 编写一段程序模拟Unix系统中的grep命令。要求用户输入一个正则表达式,然后输出在mbox.txt文件中符合这个表达式的行数。

$ python grep.py
Enter a regular expression: ˆAuthor
mbox.txt had 1798 lines that matched ˆAuthor

$ python grep.py
Enter a regular expression: ˆXmbox.
txt had 14368 lines that matched ˆX-

$ python grep.py
Enter a regular expression: java$
mbox.txt had 4218 lines that matched java$

参考代码如下:

import re
input_re = input('Enter a regular expression:')
hand = open('mbox.txt')
count = 0
for line in hand:
if re.search(input_re,line):
count = count + 1
print('mbox.txt had ' + str(count) + ' that matched ' + input_re)

练习11.2 编写一个程序查找以下格式的行,然后用findall()方法抽取每行中的数字,计算它们的平均值并输出。

New Revision: 39772
Enter file:mbox.txt
38549.7949721
Enter file:mbox-short.txt
39756.9259259

参考代码如下:

import re
filename = input('Enter file:')
hand = open(filename)
count = 0
total = 0
for line in hand:
x = re.findall('New Revision: ([0-9]+)',line)
if len(x) > 0:
count = count + 1
total = total + int(x[0])
print(total/count)

Python for Informatics 第11章 正则表达式六(译)的更多相关文章

  1. Python for Informatics 第11章 正则表达式五(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.4 转义字符 之前我们在正 ...

  2. Python for Informatics 第11章 正则表达式四(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.3 组合查询和抽取 如果我 ...

  3. Python for Informatics 第11章 正则表达式三(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.2 用正则表达式抽取数据 ...

  4. Python for Informatics 第11章 正则表达式二(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.1 正则表达式的字符匹配 ...

  5. Python for Informatics 第11章 正则表达式一(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 目前为止,我们一直在通读文件,查 ...

  6. 《python基础教程(第二版)》学习笔记 文件和素材(第11章)

    <python基础教程(第二版)>学习笔记 文件和素材(第11章) 打开文件:open(filename[,mode[,buffering]]) mode是读写文件的模式f=open(r' ...

  7. 《Python学习手册 第五版》 -第11章 赋值、表达式和打印

    上一章对Python的语句和语法已经进行了基本的说明,接下来就是每个章节的详细说明,本章的主要内容就是标题中涵盖的三点:赋值语句.表达式语句.打印语句 本章重点内容如下: 1.赋值语句 1)赋值语句的 ...

  8. Python for Infomatics 第12章 网络编程六(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 12.9 词汇表 Beautif ...

  9. [flask/python/web] 解析flask web开发(Miguel著)一书第11章主页不显示博文表单的问题

    ---------------------------------------------以下内容2017.7.14更新---------------------------------------- ...

随机推荐

  1. 创建maven工程时总是带有后缀名Maven Webapp解决办法

    做项目时突然遇到了一个新问题,从前没有的,今天不知怎么了突然有了这个问题,maven创建web项目时多出了后缀名maven webapp ,很碍眼,而且访问路径还得删了,这个后缀名才可访问,所以找了答 ...

  2. POJ2342 树形dp

    原题:http://poj.org/problem?id=2342 树形dp入门题. 我们让dp[i][0]表示第i个人不去,dp[i][1]表示第i个人去 ,根据题意我们可以很容易的得到如下递推公式 ...

  3. 线性SVM

    (本文内容和图片来自林轩田老师<机器学习技法>) 1. 线性SVM的推导 1.1 形象理解为什么要使用间隔最大化 容忍更多的测量误差,更加的robust.间隔越大,噪声容忍度越大: 1.2 ...

  4. connect 链接失败: 查找不到 signal

                提示错误是:   signal_index < 0 ;;     ----  故 connect返回false;              消除  connect  信号 ...

  5. React-native 学习记录

    在此记录下学习中的小知识 今天在componentWillUpdate调用this.setState方法,想达到一个效果:就像viewWillAppear时,系统从网络请求新的数据,并刷新界面, 但是 ...

  6. FixFFmpeg 修改官方编译的ffmpeg能在 XP 上运行的工具

    把 fixff.cmd 和 FixFFmpeg.exe 拷贝到 ffmpeg 所在的目录 运行 fixff.cmd 自动修复; fixffmpeg-20160924.7z

  7. Python 开发轻量级爬虫06

    Python 开发轻量级爬虫 (imooc总结06--网页解析器) 介绍网页解析器 将互联网的网页获取到本地以后,我们需要对它们进行解析才能够提取出我们需要的内容. 也就是说网页解析器是从网页中提取有 ...

  8. SQL的ROW_NUMBER函数

    with tabs as ( select ROW_NUMBER() over(partition by customerID order by totalPrice) as rows,custome ...

  9. [jquery]添加行内容后根据下拉菜单选择内容对比之前已有选项,若有重置再提示

    今天页面上一个添加列内容时,要对选择内容与之前已有选项内容作对比,防止用户重复选择内容 页面HTML代码 <ul class="list-group xj-list-NObor xj- ...

  10. python备忘

    1.引用已经编写好的.py文件(Windows系统) >>>import sys >>>sys.path.append("C:/python") ...