Python-ch3-数据获取与表示教学提纲

Wherearedatafrom Howtorepresentdata 数据获取与表示 DepartmentofComputerScienceandTechnologyDepartmentofUniversityBasicComputerTeaching 本地数据获取 用Python玩转数据 2 NanjingUniversity 文件的打开 4 Source f1 open r d infile txt f2 open r d outfile txt w f3 open frecord csv ab 0 file obj open filename mode r buffering 1 mode为可选参数 默认值为r buffering也为可选参数 默认值为 1 0代表不缓冲 1或大于1的值表示缓冲一行或指定缓冲区大小 Python3 x中的目录路径常常直接用类似 d test txt 或 d test txt 这样的方式表示 NanjingUniversity open 函数 mode 5 NanjingUniversity 文件相关函数 NanjingUniversity 返回值 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 将一个字符串写入文件Source NanjingUniversity 读文件 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 printp1 p2 f close NanjingUniversity 其他读写函数 file obj readlines file obj readline file obj writelines 9 File Filename companies a pyf open r companies txt cNames f readlines printcNamesf close Output GOOGLEInc n MicrosoftCorporation n AppleInc n Facebook Inc NanjingUniversity 文件读写例子 File Filename revcopy pyf1 open r companies txt cNames f1 readlines foriinrange 0 len cNames cNames i str i 1 cNames i f1 close f2 open r scompanies txt w f2 writelines cNames f2 close Output GOOGLEInc MicrosoftCorporationAppleInc Facebook Inc 将文件companies txt的字符串前加上序号1 2 3 后写到另一个文件scompanies txt中 NanjingUniversity 其他文件相关函数 file obj seek offset whence 0 在文件中移动文件指针 从whence 0表示文件头部 1表示当前位置 2表示文件尾部 偏移offset个字节 whence参数可选 默认值为0 11 File Filename companies b pys TencentTechnologyCompanyLimited f open r companies txt a f writelines n f writelines s f seek 0 0 cNames f readlines printcNamesf close NanjingUniversity 标准文件 当程序启动后 以下三种标准文件有效 1 2 3 stdinstdoutstderr 标准输入标准输出标准错误 12 Source newcName raw Enterthenameofnewcompany Enterthenameofnewcompany Alibaba printnewcName Alibaba NanjingUniversity 网络数据获取 用Python玩转数据 13 NanjingUniversity 用Python获取数据 网络数据如何获取 抓取网页 解析网页内容 urllib urllib2 httplib httplib2 14 Python3中被urllib request代替 Python3中被http client代替 NanjingUniversity 利用urllib库获取网络数据 urllib 抓取网页 解析网页内容urllib urlopen f read f readline f readlines f close 等方法 15 r urllib urlopen Source importurllib urllib urlopen在Python3用urllib request 即Python2中的urllib2 中的urlopen方法代替 NanjingUniversity yahoo财经数据 16 NanjingUniversity 利用urllib库获取yahoo财经数据 Python2 17 ifm printmprint n printlen m else print notmatch File Filename dji pyimporturllib2importredStr urllib2 urlopen m re findall dStr 2016年4月网站正则表达式更新 NanjingUniversity 利用urllib库获取yahoo财经数据 Python3 18 File Filename dji pyimporturllib requestimportredBytes urllib request urlopen 在python3中urllib read 返回bytes对象而非str 语句功能是将dStr转换成strm re findall dStr ifm print m print n print len m else print notmatch NanjingUniversity 数据形式 包含多个字符串 AXP AmericanExpressCompany 86 40 BA TheBoeingCompany 122 24 CAT CaterpillarInc 99 44 CSCO CiscoSystems Inc 23 78 CVX ChevronCorporation 115 91 19 NanjingUniversity 序列 用Python玩转数据 20 NanjingUniversity 序列 NanjingUniversity str Hello World aList 2 3 5 7 11 aTuple Sunday happy pList AXP AmericanExpressCompany 86 40 BA TheBoeingCompany 122 64 CAT CaterpillarInc 99 44 CSCO CiscoSystems Inc 23 78 CVX ChevronCorporation 115 91 21 字符串Strings列表Lists元组Tuples 22 NanjingUniversity Python中的序列 23 序列 0 N 1 N 1 2 N 2 N 2 2 N 1 NanjingUniversity 1 访问模式 元素从0开始通过下标偏移量访问 一次可访问一个或多个元素 序列相关操作 24 值比较对象身份比较布尔运算 获取重复连接判断 序列类型转换工厂函数序列类型可用内建函数 标准类型运算符 序列类型运算符 内建函数 NanjingUniversity 标准类型运算符 25 Source apple 1 3 5 2 4 6 True aTuple BA TheBoeingCompany 122 64 bTuple aTuple bTupleisnotaTupleFalse 86 40 banana False NanjingUniversity 标准类型运算符 值比较 对象身份比较 布尔运算 26 Python3 x中不再支持 NanjingUniversity 序列类型运算符 27 Source week Monday Tuesday Wednesday Thursday Friday Saturday Sunday printweek 1 week 2 n week 1 4 n week 6 n week 1 TuesdaySaturday Tuesday Wednesday Thursday Monday Tuesday Wednesday Thursday Friday Saturday Sunday Saturday Friday Thursday Wednesday Tuesday Monday apple 3 appleappleapple pine apple pineapple BA in BA TheBoeingCompany 122 64 True NanjingUniversity 序列类型运算符 NanjingUniversity 28 序列类型转换工厂函数 29 Source list Hello World H e l l o W o r l d tuple Hello World H e l l o W o r l d Python3 x中移除了该函数 因为其字符串已为Unicode字符串 NanjingUniversity 序列类型可用内建函数 30 Source aStr Hello World len aStr 13 sorted aStr H W d e l l l o o r NanjingUniversity 字符串 用Python玩转数据 31 NanjingUniversity 字符串的不同表示形式 32 lf AXP AmericanExpressCompany 86 40 BA TheBoeingCompany 122 64 CAT CaterpillarInc 99 44 CSCO CiscoSystems Inc 23 78 CVX ChevronCorporation 115 91 Source aStr TheBoeingCompany bStr TheBoeingCompany cStr TheBoeingcompany NanjingUniversity 字符串小例子 33 将字符串 Hello World 中的 World 替换成 Python 并计算其包含的标点符号 由逗号 句号 感叹号和问号组成 的个数 Output 2 File Filename puncount pyaStr Hello World bStr aStr 7 Python count 0forchinbStr ifchin count 1printcount NanjingUniversity 字符串与输出形式 34 Output Punctuationmarks 2 Output Thereare2punctuationmarks print Thereare