1.关于global声明变量的错误例子

  1. I ran across this warning:
  1. #!/usr/bin/env python2.3
  2. VAR = 'xxx'
  3.  
  4. if __name__ == '__main__':
  5. global VAR
  6. VAR = 'yyy'
  1. ---
  2.  
  3. OUTPUT:
  4.  
  5. ./var.py:0: SyntaxWarning: name 'VAR' is assigned to before global declaration
  6.  
  7. ----
  8.  
  9. But, a little twiddle quiets the warning, and I have no idea why:
  1. #!/usr/bin/env python2.3
  2. VAR = 'xxx'
  3.  
  4. def set_var():
  5. global VAR
  6. VAR = 'yyy'
  7.  
  8. if __name__ == '__main__':
  9. set_var()
  1. ---
  2.  
  3. No output.
  1. Global is normally used within a function definition to allow it to assign
  2. to names defined outside the function (as in your 2nd example). In your
  3. first example global is outside any function definition, and therefore not
  4. meaningful, as well as giving a SyntaxWarning.

2.HTMLParser中feed

HTMLParser的feed()方法会调用

handle_starttag(), handle_data(), handle_endtag()方法

  1. #! /usr/bin/env python
  2.  
  3. #coding=utf-8
  4.  
  5. from htmlentitydefs import entitydefs
  6.  
  7. from HTMLParser import HTMLParser
  8.  
  9. import sys
  10.  
  11. class TitleParser(HTMLParser):
  12.  
  13. def __init__(self):
  14.  
  15. self.title = ' '
  16.  
  17. self.readingtitle = 0
  18.  
  19. HTMLParser.__init__(self)
  20.  
  21. def handle_starttag(self, tag, attrs):
  22.  
  23. if tag == 'title':
  24.  
  25. self.readingtitle = 1
  26.  
  27. def handle_data(self, data):
  28.  
  29. if self.readingtitle:
  30.  
  31. self.title += data
  32.  
  33. def handle_endtag(self, tag):
  34.  
  35. if tag == 'title':
  36.  
  37. self.readingtitle = 0
  38.  
  39. def handle_entityref(self, name):
  40.  
  41. if entitydefs.has_key(name):
  42.  
  43. self.handle_data(entitydefs[name])
  44.  
  45. else:
  46.  
  47. self.handle_data('&' + name + ';')
  48.  
  49. def gettitle(self):
  50.  
  51. return self.title
  52.  
  53. fd = open(sys.argv[1])
  54.  
  55. tp = TitleParser()
  56.  
  57. tp.feed(fd.read())
  58.  
  59. 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    处理一些特殊字符,以&开头的,比如 &nbsp;
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格式,具体使用如下

  1. #!/usr/bin/env python
  2.  
  3. # -*- coding:utf-8 -*-
  4.  
  5. import csv
  6.  
  7. with open('egg2.csv', 'wb') as csvfile:
  8.  
  9. spamwriter = csv.writer(csvfile, delimiter=' ',quotechar='|', quoting=csv.QUOTE_MINIMAL)
  10.  
  11. spamwriter.writerow(['a', '', '', '', ''])
  12.  
  13. spamwriter.writerow(['b', '', '', '', ''])
  14.  
  15. spamwriter.writerow(['c', '', '', '', ''])
  16.  
  17. spamwriter.writerow(['d', '','','', ''])
  18.  
  19. spamwriter.writerow(['e', '','','', ''])

or

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3.  
  4. import csv
  5. with open('egg2.csv', 'wb') as csvfile:
  6. spamwriter = csv.writer(csvfile,dialect='excel')
  7. spamwriter.writerow(['a', '', '', '', ''])
  8. spamwriter.writerow(['b', '', '', '', ''])
  9. spamwriter.writerow(['c', '', '', '', ''])
  10. spamwriter.writerow(['d', '','','', ''])
  11. spamwriter.writerow(['e', '','','', ''])

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

5.正则表达式中.*?

在正则表达式中使用.*?匹配字符时,要注意其不包含\n,当中间含有换行时,可使用(.|\n)*?进行匹配

Python学习笔记5的更多相关文章

  1. python学习笔记整理——字典

    python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...

  2. VS2013中Python学习笔记[Django Web的第一个网页]

    前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...

  3. python学习笔记之module && package

    个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...

  4. python学习笔记(六)文件夹遍历,异常处理

    python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...

  5. python学习笔记--Django入门四 管理站点--二

    接上一节  python学习笔记--Django入门四 管理站点 设置字段可选 编辑Book模块在email字段上加上blank=True,指定email字段为可选,代码如下: class Autho ...

  6. python学习笔记--Django入门0 安装dangjo

    经过这几天的折腾,经历了Django的各种报错,翻译的内容虽然不错,但是与实际的版本有差别,会出现各种奇葩的错误.现在终于找到了解决方法:查看英文原版内容:http://djangobook.com/ ...

  7. python学习笔记(一)元组,序列,字典

    python学习笔记(一)元组,序列,字典

  8. Pythoner | 你像从前一样的Python学习笔记

    Pythoner | 你像从前一样的Python学习笔记 Pythoner

  9. OpenCV之Python学习笔记

    OpenCV之Python学习笔记 直都在用Python+OpenCV做一些算法的原型.本来想留下发布一些文章的,可是整理一下就有点无奈了,都是写零散不成系统的小片段.现在看 到一本国外的新书< ...

  10. python学习笔记(五岁以下儿童)深深浅浅的副本复印件,文件和文件夹

    python学习笔记(五岁以下儿童) 深拷贝-浅拷贝 浅拷贝就是对引用的拷贝(仅仅拷贝父对象) 深拷贝就是对对象的资源拷贝 普通的复制,仅仅是添加了一个指向同一个地址空间的"标签" ...

随机推荐

  1. 什么是B-Tree

    B-Tree就是我们常说的B树,一定不要读成B减树,否则就很丢人了.B树这种数据结构常常用于实现数据库索引,因为它的查找效率比较高. B-Tree与二叉查找树的对比 我们知道二叉查找树查询的时间复杂度 ...

  2. mongodb中limit与skip方法

    Mongodb Limit()方法 如果需要在mongodb中获取指定数量的数据记录,这时候就要用到limit()方法,该方法需要接收一个数字参数 基本语法:  DB.COLLECTION_NAME. ...

  3. 连接池报错 Proxool Provider unable to load JAXP configurator file: proxool.xml

    上篇博文讲到简易配置 proxool 连接池:http://www.cnblogs.com/linnuo/p/7232380.html 由于把说明注释留在了 proxool.xml 配置文件里导致配置 ...

  4. DDD理论学习系列(13)-- 模块

    DDD理论学习系列--案例及目录 1. 引言 Module,即模块,是指提供特定功能的相对独立的单元.提到模块,你肯定就会想到模块化设计思想,也就是功能的分解和组合.对于简单问题,可以直接构建单一模块 ...

  5. Hbase 基础 - shell 与 客户端

    版权说明:  本文章版权归本人及博客园共同所有,转载请标明原文出处(http://www.cnblogs.com/mikevictor07/),以下内容为个人理解,仅供参考. 一.简介 Hbase是在 ...

  6. 【整理】01. Fiddler 杂记

    抓手机包步骤: Tools -- Fiddler Options -- Connections (默认)Fiddler listens on port:8888 (勾选)Allow remote co ...

  7. Codeforces 828B Black Square(简单题)

    Codeforces 828B Black Square(简单题) Description Polycarp has a checkered sheet of paper of size n × m. ...

  8. NET中解决KafKa多线程发送多主题的问题

    一般在KafKa消费程序中消费可以设置多个主题,那在同一程序中需要向KafKa发送不同主题的消息,如异常需要发到异常主题,正常的发送到正常的主题,这时候就需要实例化多个主题,然后逐个发送. 在NET中 ...

  9. vue指令v-html示例解析

    更新元素的innerHTML,不会作为vue模板编译,可用组件来代替. 在网站上动态渲染任意 HTML 是非常危险的,因为容易导致 xss攻击.只在可信内容上使用 v-html,永不用在用户提交的内容 ...

  10. 数据结构二叉树的所有基本功能实现。(C++版)

    本人刚学数据结构,对树的基本功能网上找不到C++代码 便自己写了一份,贴出方便大家进行测试和学习. 大部分功能未测试,如有错误或者BUG,请高手们指教一下,谢谢. 结点声明: BinTreeNode. ...