python ch3 数据获取与表示

上传人:xh****66 文档编号:58620424 上传时间:2018-10-31 格式:PPTX 页数:56 大小:2.05MB
返回 下载 相关 举报
python ch3 数据获取与表示_第1页
第1页 / 共56页
python ch3 数据获取与表示_第2页
第2页 / 共56页
python ch3 数据获取与表示_第3页
第3页 / 共56页
python ch3 数据获取与表示_第4页
第4页 / 共56页
python ch3 数据获取与表示_第5页
第5页 / 共56页
点击查看更多>>
资源描述

《python ch3 数据获取与表示》由会员分享,可在线阅读,更多相关《python ch3 数据获取与表示(56页珍藏版)》请在金锄头文库上搜索。

1、,Where are data from?How to represent data?,数据 获取与表示,Department of Computer Science and Technology Department of University Basic Computer Teaching,本地数据获取,用Python玩转数据,2,Nanjing University,用Python获取数据,本地数据如何获取?文件的打开,读写和关闭 打开后才能进行读写 读文件 写文件 文件为什么需要关闭?,3,Nanjing University,文件的打开,4,Source f1 = open(rd:i

2、nfile.txt) f2 = open(rd:outfile.txt, w) f3 = open(frecord.csv, ab, 0),file_obj = open(filename, mode=r, buffering=-1) mode为可选参数,默认值为r buffering也为可选参数,默认值为-1(0代表不缓冲,1 或大于1的值表示缓冲一行或指定缓冲区大小),Python 3.x中的目录 路径常常直接用类似 “”d:test.txt“”或 “d:/test.txt”这样的 方式表示,Nanjing University,open()函数-mode,5,Nanjing Univer

3、sity,文件相关函数,Nanjing University,返回值 open()函数返回一个文件(file)对象 文件对象可迭代 有关闭方法和许多读写相关的方法/函数 f.read(), f.write(), f.readline(), f.readlines(), f.writelines() f.close() f.seek(),6,写文件-f.write(),firstpro.txt : Hello, World!,7, f = open(firstpro.txt, w) f.write(Hello, World!) f.close(),file_obj.write(str) 将一个字

4、符串写入文件Source,Nanjing University,读文件-f.read(),file_obj.read(size) 从文件中至多读出size字节数据,返回一个字符串 file_obj.read() 读文件直到文件结束,返回一个字符串,Output: Hello, World!,8,Source f = open(firstpro.txt) p1 = f.read(5) p2 = f.read() print p1,p2 f.close(),Nanjing University,其他读写函数,file_obj.readlines() file_obj.readline() file

5、_obj.writelines(),9,File # Filename: companies_a.py f = open(rcompanies.txt) cNames = f.readlines() print cNames f.close(),Output: GOOGLE Inc.n, Microsoft Corporationn, Apple Inc.n, Facebook, Inc.,Nanjing University,文件读写例子,File # Filename: revcopy.py f1= open(rcompanies.txt) cNames = f1.readlines()

6、for i in range(0,len(cNames): cNamesi = str(i+1) + + cNamesi f1.close() f2 = open(rscompanies.txt,w) f2.writelines(cNames) f2.close(),Output: GOOGLE Inc. Microsoft Corporation Apple Inc. Facebook, Inc.,将文件companies.txt 的字符串前加上序号1、2、3、后写到 另一个文件scompanies.txt中。,Nanjing University,其他文件相关函数,file_obj.see

7、k(offset , whence=0), 在文件中移动文件指针,从 whence(0表示文件头部,1表示 当前位置,2表示文件尾部)偏 移offset个字节 whence参数可选,默认值为0,11,File # Filename: companies_b.py s = Tencent Technology Company Limited f = open(rcompanies.txt , a+) f.writelines(n) f.writelines(s) f.seek(0,0) cNames = f.readlines() print cNames f.close(),Nanjing U

8、niversity,标准文件, 当程序启动后,以下三种标准文件有效,1,2,3,stdin stdout stderr,标准输入 标准输出 标准错误,12,Source newcName = raw_input(Enter the name of new company: ) Enter the name of new company: Alibaba print newcName Alibaba,Nanjing University,网络数据获取,用Python玩转数据,13,Nanjing University,用Python获取数据,网络数据如何获取? 抓取网页,解析网页内容,urlli

9、b,urllib2,httplib ,httplib2,14,Python 3中被 urllib.request代替,Python 3中被 http.client代替,Nanjing University,利用urllib库获取网络数据,urllib,抓取网页,解析网页内容 urllib.urlopen() f.read(), f.readline(), f.readlines(),f.close()等方法,15, r = urllib.urlopen(http:/ html = r.read() html,Source import urllib,urllib.urlopen在Python

10、3用urllib.request(即 Python 2中的urllib2) 中的urlopen方法代替,Nanjing University,yahoo财经数据,16,http:/ University,利用urllib库获取yahoo财经数据(Python 2),17,if m: print m print n print len(m) else: print not match,File# Filename: dji.py import urllib2 import re dStr = urllib2.urlopen(http:/ m = re.findall( (.*?) (.*?).*

11、?(.*?).*?, dStr),m = re.findall( (.*?)(.*?).*? (.*?).*?, dStr) (2016年4月网站正则表达式更新),Nanjing University,利用urllib库获取yahoo财经数据(Python 3),18,File # Filename: dji.py import urllib.request import re dBytes = urllib.request.urlopen(http:/ dStr = dBytes.decode(GBK) #在python3中urllib.read()返回bytes对象而非str,语句功能是将

12、dStr转换成str m = re.findall(.*?)(.*?).*?(.*?).*?, dStr) if m: print(m) print (n) print (len(m) else: print (not match),Nanjing University,数据形式, 包含多个字符串,AXP, American Express Company, 86.40 BA, The Boeing Company, 122.24 CAT, Caterpillar Inc., 99.44 CSCO, Cisco Systems, Inc., 23.78 CVX, Chevron Corpora

13、tion, 115.91 ,19,Nanjing University,序列,用Python玩转数据,20,Nanjing University,序列,Nanjing University,str = Hello, World! aList = 2, 3, 5, 7, 11 aTuple = (Sunday, happy ) pList = (AXP, American Express Company, 86.40), (BA, The Boeing Company, 122.64), (CAT, Caterpillar Inc., 99.44), (CSCO, Cisco Systems,

14、Inc., 23.78) (CVX, Chevron Corporation, 115.91),21,字符串 Strings列表 Lists元组 Tuples,22,Nanjing University,Python中的序列,23,序列,0,-N,1,-(N-1),2,-(N-2),N-2,-2,N-1,Nanjing University,-1,访问模式, 元素从0开始通过下标偏移 量访问 一次可访问一个或多个元素,序列相关操作,24,值比较 对象身份比较 布尔运算,获取 重复 连接 判断,序列类型转换工厂函数 序列类型可用内建函数,标准 类型 运算符,序列 类型 运算符,内建 函数,Nanjing University,标准类型运算符,25,Source apple 1,3,5 != 2,4,6 True aTuple = (BA, The Boeing Company, 122.64) bTuple = aTuple bTuple is not aTuple False (86.40 banana) False,

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号