大数据处理之道(十分钟学会Python)
(0)目录 快速学Python 和 易犯错误(文本处理) Python文本处理和Java/C比对 十分钟学会Python的基本类型 快速学会Python(实战) 大数据处理之道(十分钟学会Python) 一:python 简介 (1)Python的由来 Python(英语发音:/?pa?θ?n/),是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991 年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结 在一起。常见的一种应用情形是,使用Python快速生成程序的原型(有时甚至是程序的最终界面),然后对其中有特别要求的部分,用更合适的语言改写, 比 如3D游戏中的图形渲染模块,性能要求特别高,就可以用C++重写。 (2)Python 语法简介 ---- 类型转化 int(x [,base ]) ? ? ? ? 将x转换为一个整数 (3)Python 语法简介 ---- 类型转化 s + r ? ? ? ? ? ? ? ? ? 序列连接 (4)(3)Python 语法简介 ---- 类型转化 x >> y ? ? ? ? ? ? ? ? ?右移 二:python应用 (1) 文件处理
filename = raw_input('Enter your file name') #输入要遍历读取的文件路径及文件名 file = open(filename,'r') done = 0 while not done: aLine = file.readline() if(aLine != ''): print aLine,else: done = 1 file.close() #关闭文件解释: .readline() 和 .readlines() 之间的差异是后者一次读取整个文件,.readlines() 自动将文件内容分析成一个行的列表,该列表可以由 Python 的 for ... in ... 结构 进行处理。另一方面,.readline() 每次只读取一行,通常比 .readlines() 慢得多。仅当没有足够内存可以一次读取整个文件时,才应该使用 .readline()。 如果Python文件读到了文件尾,则会返回一个空字符串‘’,而如果是读到一个空行的话,则会返回一个‘n’ Python的readline()方法,每行最后都会加上一个换行字符‘n’。有时候有的文件最后一行没有以'n'结尾时,不返回‘n’。 readlines()方法返回的是一个列表,而readline()返回一个字符串。(2)错误处理 Python报错TypeError: 'str' object is not callable当一般内部函数被用作变量名后可能出现此错误。比如: range=1 for i in range(0,1): ……… 就会报这样的错误 这样的错会报在for行,但是时间引起的原因却是在range=1这行,如果两行相距较远,怎很难被发现。 所以要特别注意不要用内部已有的变量和函数名作自定义变量名。或者str被预先定义了 str=10 for i in range(1,10): ? print str(i) (3) 综合应用,文件读取,控制台读取,时间转化,编码转换 import time from time import strftime import sys reload(sys) sys.setdefaultencoding('utf8') # -*- coding: cp936 -*- print ("Hello,Python!") #!/usr/bin/python a = 21 b = 10 c = 0 c = a + b print "Line 1 - Value of c is ",c c = a - b print "Line 2 - Value of c is ",c c = a * b print "Line 3 - Value of c is ",c c = a / b print "Line 4 - Value of c is ",c c = a % b print "Line 5 - Value of c is ",c a = 2 b = 3 c = a**b print "Line 6 - Value of c is ",c a = 10 b = 5 c = a//b print "Line 7 - Value of c is ",c # for repeat its list = [2,4,6,8] sum = 0 for num in list: sum = sum + num print("The sum is:",sum) # print and Input,assignment print("Hello,I'm Python!") name = input('What is your name?n') print('Hi,%s.' % name) # test for fruits = ['Banana','Apple','Lime'] loud_fruits = [fruit.upper() for fruit in fruits] print(loud_fruits) # open,write and read file fo = open("./tmp/foo.txt","w+") fo.write("Python is a gerat language.nYeah its great!!nI am zhang yapeng,who are you?n") t_str = u'我是张燕鹏,您是什么货色?' print(t_str) fo.write(t_str) fo.close() #read and write fr = open("./tmp/foo1.txt","r+") fw = open("foo_rw.txt","wb") done = 0; localtime = time.asctime(time.localtime(time.time())) print "Local current time : ",localtime fw.write(localtime + "n") while not done: t_str = fr.readline() if(t_str != ''): print "Read String is : ",t_str fw.write(t_str) else: done = 1 fr.close() fw.close() # test time (import) localtime = time.localtime(time.time()) print "Local current time : ",localtime # format the time from time import strftime t_time = strftime( '%Y-%m-%d %H:%M:%S',localtime) print "formatting local current time : ",t_time # design the time by yourself year = str(localtime.tm_year) mon = str(localtime.tm_mon) day = str(localtime.tm_mday) hour = str(localtime.tm_hour) mins = str(localtime.tm_min) sec = str(localtime.tm_sec) newtime = u"时间是: " + year + "年" + mon + "月" + day + "日 " + hour + ":" + mins + ":" + sec print "Local current time : ",newtime (4)运行图: (5) 总结: (1)Python是一门入手非常快的语言,处理大数据的好语言,一些规范非常类似于c++语言,例如语法和一些函数命名,文件的打开和读写,以及 读写方式,非常类似于c++ (2)正如,开头所写的 “python是胶水语言,使用Python快速生成程序的原型(有时甚至是程序的最终界面),然后对其中有特别要求的部分,用更合适 的语言改写,比如3D游戏中的图形渲染模块,性能要求特别高,就可以用C++重写。” (3)分享一下非常基础的系统的学习网站 ?? (4)W3CSchool.cc (3)中提到的学习网站是非常基础的人们课程,要是想深入,具体的内容可以百度 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |