收藏 分享(赏)

PythonContextCH05.pptx

上传人:weiwoduzun 文档编号:4301426 上传时间:2018-12-21 格式:PPTX 页数:14 大小:55.72KB
下载 相关 举报
PythonContextCH05.pptx_第1页
第1页 / 共14页
PythonContextCH05.pptx_第2页
第2页 / 共14页
PythonContextCH05.pptx_第3页
第3页 / 共14页
PythonContextCH05.pptx_第4页
第4页 / 共14页
PythonContextCH05.pptx_第5页
第5页 / 共14页
点击查看更多>>
资源描述

1、Python Programming in Context,Chapter 5,Objectives,To use text files to store large data sets To access online data sources To introduce the while loop To introduce Boolean expressions,Text File,Sequence of characters, organized into lines, stored on some external memory device (such as a hard drive

2、) End of line markers (new line character),Python,Open a file open Read from a file read readline readlines Write to a file Write,Python,Iterate thru the file line by line Use split method to process the line,Listing 5.1,rainfile = open(“rainfall.txt“,“r“)for aline in rainfile:values = aline.split()

3、print(values0, “had“,values1,“inches of rain.“)rainfile.close(),Listing 5.2,rainfile = open(“rainfall.txt“,“r“) outfile = open(“rainfallInCM.txt“,“w“)for aline in rainfile:values = aline.split()inches = float(values1)cm = 2.54 * inchesoutfile.write(values0+“ “+str(cm)+“n“) rainfile.close() outfile.c

4、lose(),Processing Earthquake Data,2.8 2006/10/19 02:02:10 62.391 -149.751 15.0 CENTRAL ALASKA 2.5 2006/10/19 00:31:15 20.119 -156.213 1.5 MAUI REGION, HAWAII 5.0 2006/10/18 21:15:51 4.823 -82.592 37.3 SOUTH OF PANAMA 2.6 2006/10/18 21:12:25 59.934 -147.904 30.0 GULF OF ALASKA 3.4 2006/10/18 20:59:21

5、 36.540 -89.640 7.7 SOUTHEASTERN MISSOURI 2.7 2006/10/18 20:11:22 61.023 -151.418 60.0 SOUTHERN ALASKA 3.1 2006/10/18 16:40:15 20.282 -156.611 4.7 MAUI REGION, HAWAII 2.7 2006/10/18 14:12:19 59.808 -152.538 50.0 SOUTHERN ALASKA 2.8 2006/10/18 14:02:12 60.686 -151.871 90.0 KENAI PENINSULA, ALASKA 4.9

6、 2006/10/18 12:10:01 1.758 127.488 127.0 HALMAHERA, INDONESIA 6.2 2006/10/18 10:45:36 -15.081 167.243 138.5 VANUATU,Listing 5.3,def makeMagnitudeList():quakefile = open(“earthquakes.txt“,“r“)maglist = for aline in quakefile:vlist = aline.split()maglist.append(float(vlist0)return maglist,Processing D

7、ata from the Internet,urllib.request module Internet behaves just like a text file,Listing 5.4,import urllib.requestdef countHead(url):page = urllib.request.urlopen(url)numHeadLines = 0line = page.readline().decode(utf-8) while not in line:line = page.readline().decode(utf-8)line = page.readline().d

8、ecode(utf-8) while not in line: numHeadLines = numHeadLines + 1line = page.readline().decode(utf-8) line = page.readline().decode(utf-8) while “ not in line: line = page.readline().decode(utf-8) line = page.readline().decode(utf-8) while line != “ and “ not in line: print (line:-1)line = page.readli

9、ne().decode(utf-8) print (“number of lines in header = “, numHeadLines)page.close(),Correlating Data from Internet,Using real data streams Stock market data,Listing 5.5,def correlation(xlist, ylist):xbar = mean(xlist)ybar = mean(ylist)xstd = standardDev(xlist)ystd = standardDev(ylist)num = 0.0for i

10、in range(len(xlist):num = num + (xlisti-xbar) * (ylisti-ybar)corr = num / (len(xlist)-1) * xstd * ystd) return corr,Listing 5.6,def stockCorrelate(ticker1, ticker2):url1 = urllib.request.urlopen(http:/ url2 = urllib.request.urlopen(http:/ t1Data = url1.readlines()t2Data = url2.readlines() t1Data = l

11、ine0:-1.decode(“utf-8“).split(,) for line in t1Data1: t2Data = line0:-1.decode(“utf-8“).split(,) for line in t2Data1: t1Close = t2Close = for i in range(min(len(t1Data), len(t2Data): if t1Datai0 = t2Datai0:t1Close.append(float(t1Datai4)t2Close.append(float(t2Datai4) return correlation(t1Close, t2Close),

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 网络科技 > Python

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报