易截截图软件、单文件、免安装、纯绿色、仅160KB

正则表达式与python

 在Python中有一个非常重要也非常好用的模块re,在import re后,就能够在Python中使用正则表达式,源于此次项目要用正则表达式对html代码提取一定的字符,所以在这也就用些小例子来熟悉一下正则表达式
现在就用最简单的例子
import re
s='<title>http://www.baidu.com</title>'
print re.findall(r'<\w+>(.+)</',s)
运行后结果为
>>>
['http:\\www.baidu.com']
这个相对来说还是比较简单的,但是这个正则表达式还是有比较多的问题
1对于比较复杂的字符串,比如嵌套了标签的字符串,就没有办法了,因为只能够判断最外一层的<></>标记而已
2是这个是判断具有类似<></>标记的字符串,对于实际的html中的提取,还是要加上具体的值,比如是title,还是head
import re
s='<head><title>http:\\www.baidu.com</title></head>'
print re.findall(r'title>(.+)</title',s)
运行后得到
>>>
['http:\\www.baidu.com']
虽然在这个比较简单的代码中我们解决了上述两个问题,但是针对html中更加复杂的代码,我觉得还是会有很多的问题
不过今天也就是简单的熟悉一下正则表达式,所以也就不再去深入研究,经过队员的讨论后在探讨解决问题的方案
下面给出一个判断邮箱地址是否合法的正则表达式
邮箱主要包括@和.,所以在判断的时候也只需假如这两个条件就可以了
import re
s='zhuangruln@gmail.com  zhuangasdsad@126.com zhusdandsai@adsd'
print re.findall(r'(\w+@\w+\.\w+)',s)
运行结果
['zhuangruln@gmail.com', 'zhuangasdsad@126.com']
>>>


相关文档:

Python几种并发实现方案的性能比较

 
#!/Library/Frameworks/Python.framework/Versions/2.5/bin/python
# encoding: utf-8
import sys, time
import thread
 
SLEEP_TIME = 0.0001
 
def run_benchmark(n, m):
    # print(">> Python 2.5.1, stackless 3.1b3 here (N=%d, M=%d)!\n" % (n, m))
    lock ......

Python监视进程


由subprocess创建一个进程,然后进行监视
每一秒钟查看一次,如果正在运行,打印pid和running,如果已停止,,继续执行任务并打印Termined
shell和stdout均设置为False
也许这对做病毒的守护进程很好
#!/usr/bin/env python
import subprocess , sys , time
p=subprocess.Popen(['ping','127.0.0.1','-n','10'], she ......

Python 的C语言扩展

操作系统:linux debian 4.0, python版本2.5
s1:安装python2.5-dev。因为Python.h是在dev包中才有。
test@debian:~/test_python_c$ aptitude search python2.5-dev
p python2.5-dev - Header files and a static library for Python.
test@debian:~/test_python_c$ sudo aptitude install python2 ......

Python & XML


Chapter 1
Python and XML
Python and XML are two very different animals, each with a rich
history. Python is a full-scale programming language that has grown
from scripting world roots in a very organic way, through the vision
and guidance of Python's inventor, Guido van Rossum. Guido continue ......

Python字符串操作

 #Python字符串操作
''
'1.复制字符串'
''
#strcpy(
sStr1,
sStr2)
sStr1 =
'strcpy'
sStr2 =
sStr1
sStr1 =
'strcpy2'
print
sStr2
''
'2.连接字符串'
''
#strcat(
sStr1,
sStr2)
sStr1 =
'strcat'
sStr2 =
'append'
sStr1 +
=
sStr2
print
sStr1
''
'3.查找字符'
''
#strc ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号