1、Python之文件操作:http://blog.chinaunix.net/uid-26602509-id-3503138.html
2、正则表达式教程:
http://deerchao.net/tutorials/regex/regex.htm
http://www.java3z.com/cwbwebhome/article/article8/Regex/Java.Regex.Tutorial.html
http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html
如果要处理数据,正则表达式是必须要熟练掌握的。现在两眼一抹黑,看不懂下面的代码是如何通过正则表达式获取数据的:
import urllib
import re
dStr = urllib.urlopen('http://finance.yahoo.com/q/cp?s=%5EDJI+Components').read()
m = re.findall('<tr><td class=\"yfnc_tabledata1\"><b><a href=\".*?\">\
(.*?)</a></b></td><td class=\"yfnc_tabledata1\">(.*?)</td>.*?<b>(.*?)</b>.*?</tr>', dStr)
if m:
print m
print '\n'
print len(m)
else:
print 'not match'
暂停第2周学习,先把正则表达式搞定。
3、编程小练习:
创建一个文件src.txt,文件内容为:
How many seas must a white dove sail
Before she sleeps in the sand
将src.txt的内容复制到文件dest.txt中,并在dest.txt文件头部添加另两行字符串,添加后dest.txt文件中的内容为:
How many roads must a man walk down
Before they call him a man
How many seas must a white dove sail
Before she sleeps in the sand
f1 = open(r'D:\src.txt', 'w+')
f1.writelines('How many seas must a white dove sail\nBefore she sleeps in the sand\n')
f1.seek(0, 0)
f2 = open(r'D:\dest.txt', 'w')
f2.writelines('How many roads must a man walk down\n')
f2.writelines('Before they call him a man\n')
words = f1.readlines()
for w in words:
f2.writelines(w)
f1.close()
f2.close()