《用Python玩转数据》第2周学习笔记(Part1)

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()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值