Python Challenge第二关

连续两天加班到11点,也是一件挺累的事。忙中偷闲,做了下Python Challenge的第二题,很快就做完了,正好是这几天看了些正则的东西。

第二题的提示很简单:

recognize the characters. maybe they are in the book,
but MAYBE they are in the page source.

ok,是让我们看page source,源码最后面有一段一千多行的字符串,就是像这样的:%%$@_$^__#)^)。

还有一段提示是这样的:find rare characters in the mess below。

先把这段长长的字符串保存到一个txt中,然后解析。源码如下:

import re
f = open('ocr.txt','r')
content = f.read()
print re.findall(r'[a-zA-Z]',content)
运行python ocr.py,结果为:
lyoe@ubuntu:~$ python ocr.py
['e', 'q', 'u', 'a', 'l', 'i', 't', 'y']
我们已经得到第三题的地址了:http://www.pythonchallenge.com/pc/def/equality.html
主要是学习re的用法。
Help on function findall in module re:
findall(pattern, string, flags=0)
Return a list of all non-overlapping matches in the string.
If one or more groups are present in the pattern, return a
list of groups; this will be a list of tuples if the pattern
has more than one group.
Empty matches are included in the result.
,

Leave a Reply