你好,游客 登录
背景:
阅读新闻

大数据处理之道(十分钟学会Python)

[日期:2015-01-22] 来源:CSDN博客  作者:瞭望天空 [字体: ]

一:python 简介

(1)Python的由来

Python(英语发音:/ˈpaɪθən/), 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991

年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结

在一起。常见的一种应用情形是,使用Python快速生成程序的原型(有时甚至是程序的最终界面),然后对其中有特别要求的部分,用更合适的语言改写,

比如3D游戏中的图形渲染模块,性能要求特别高,就可以用C++重写

(2)Python 语法简介 ---- 类型转化

int(x [,base ])         将x转换为一个整数
long(x [,base ])        将x转换为一个长整数
float(x )               将x转换到一个浮点数
complex(real [,imag ])  创建一个复数
str(x )                 将对象 x 转换为字符串
repr(x )                将对象 x 转换为表达式字符串

eval(str )              用来计算在字符串中的有效Python表达式,并返回一个对象
tuple(s )               将序列 s 转换为一个元组
list(s )                将序列 s 转换为一个列表
chr(x )                 将一个整数转换为一个字符
unichr(x )              将一个整数转换为Unicode字符
ord(x )                 将一个字符转换为它的整数值
hex(x )                 将一个整数转换为一个十六进制字符串
oct(x )
             将一个整数转换为一个八进制字符串

(3)Python 语法简介 ---- 类型转化

s + r                   序列连接
s * n , n * s           s的 n 次拷贝,n为整数
s % d                   字符串格式化(仅字符串)

s[i]                    索引
s[i :j ]                切片
x in s , x not in s     从属关系
for x in s :            迭代
len(s)                  长度
min(s)                  最小元素
max(s)                  最大元素
s[i ] = x               为s[i]重新赋值
s[i :j ] = r            将列表片段重新赋值
del s[i ]               删除列表中一个元素

del s[i :j ]            删除列表中一个片段

(4)(3)Python 语法简介 ---- 类型转化

x >> y                  右移
x & y                   按位与
x | y                   按位或
x ^ y                   按位异或 (exclusive or)
~x                      按位翻转
x + y                   加
x - y                   减
x * y                   乘
x / y                   常规除
x // y                  地板除
x ** y                  乘方 (xy )

x % y                   取模 (x mod y )
-x                      改变操作数的符号位
+x                      什么也不做
~x                      ~x=-(x+1)
abs(x )                 绝对值
divmod(x ,y )           返回 (int(x / y ), x % y )
pow(x ,y [,modulo ])    返回 (x ** y ) x % modulo
round(x ,[n])           四舍五入,n为小数点位数

x < y                   小于
x > y                   大于
x == y                  等于
x != y                  不等于(与<>相同)

x >= y                  大于等于
x <= y                  小于等于

二:python应用

(1) 文件处理

 

[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. filename = raw_input('Enter your file name'#输入要遍历读取的文件路径及文件名 
  2. file = open(filename,'r'
  3. done = 0 
  4. while not  done: 
  5.         aLine = file.readline() 
  6.         if(aLine != ''): 
  7.             print aLine, 
  8.         else
  9.             done = 1 
  10. 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) 综合应用,文件读取,控制台读取,时间转化,编码转换

[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. import time 
  2. from time import strftime 
  3. import sys 
  4. reload(sys) 
  5. sys.setdefaultencoding('utf8'
  6. # -*- coding: cp936 -*- 
  7. print ("Hello, Python!"
  8. #!/usr/bin/python 
  9. a = 21 
  10. b = 10 
  11. c = 0 
  12.  
  13. c = a + b 
  14. print "Line 1 - Value of c is ", c 
  15.  
  16. c = a - b 
  17. print "Line 2 - Value of c is ", c   
  18.  
  19. c = a * b 
  20. print "Line 3 - Value of c is ", c   
  21.  
  22. c = a / b 
  23. print "Line 4 - Value of c is ", c   
  24.  
  25. c = a % b 
  26. print "Line 5 - Value of c is ", c 
  27.  
  28. a = 2 
  29. b = 3 
  30. c = a**b   
  31. print "Line 6 - Value of c is ", c 
  32.  
  33. a = 10 
  34. b = 5 
  35. c = a//b   
  36. print "Line 7 - Value of c is ", c 
  37. # for repeat its 
  38. list = [2468
  39. sum = 0 
  40. for num in list: 
  41.     sum = sum + num 
  42. print("The sum is:", sum) 
  43. # print and Input, assignment 
  44. print("Hello, I'm Python!"
  45.  
  46. name = input('What is your name?\n'
  47. print('Hi, %s.' % name) 
  48.  
  49. # test for 
  50. fruits = ['Banana''Apple''Lime'
  51. loud_fruits = [fruit.upper() for fruit in fruits] 
  52. print(loud_fruits) 
  53.  
  54. # open, write and read file 
  55. fo = open("./tmp/foo.txt","w+"
  56. fo.write("Python is a gerat language.\nYeah its great!!\nI am zhang yapeng, who are you?\n"
  57. t_str = u'我是张燕鹏,您是什么货色?' 
  58. print(t_str) 
  59. fo.write(t_str) 
  60. fo.close() 
  61.  
  62. #read and write 
  63. fr = open("./tmp/foo1.txt","r+"
  64. fw = open("foo_rw.txt","wb"
  65. done = 0
  66. localtime = time.asctime(time.localtime(time.time())) 
  67. print "Local current time : ", localtime 
  68. fw.write(localtime + "\n"
  69. while not done: 
  70.     t_str = fr.readline() 
  71.     if(t_str != ''): 
  72.         print "Read String is : ", t_str 
  73.         fw.write(t_str) 
  74.     else
  75.         done = 1 
  76. fr.close() 
  77. fw.close() 
  78.  
  79. # test time (import) 
  80. localtime = time.localtime(time.time()) 
  81. print "Local current time : ", localtime 
  82. # format the time from time import strftime 
  83. t_time = strftime( '%Y-%m-%d %H:%M:%S', localtime) 
  84. print "formatting local current time : ", t_time 
  85. # design the time by yourself 
  86. year = str(localtime.tm_year) 
  87. mon = str(localtime.tm_mon) 
  88. day = str(localtime.tm_mday) 
  89. hour = str(localtime.tm_hour) 
  90. mins = str(localtime.tm_min) 
  91. sec = str(localtime.tm_sec) 
  92. newtime = u"时间是: " + year + "年" + mon + "月" + day + "日 " + hour + ":" + mins + ":" + sec 
  93. print "Local current time : ", newtime 

(4)运行图:




(5) 总结:

(1)Python是一门入手非常快的语言,处理大数据的好语言,一些规范非常类似于c++语言,例如语法和一些函数命名,文件的打开和读写,以及

读写方式,非常类似于c++

(2)正如,开头所写的 “python是胶水语言,使用Python快速生成程序的原型(有时甚至是程序的最终界面),然后对其中有特别要求的部分,用更合适

语言改写,如3D游戏中的图形渲染模块,性能要求特别高,就可以用C++重写。

(3)分享一下非常基础的系统的学习网站  

(4)W3CSchool.cc (3)中提到的学习网站是非常基础的人们课程,要是想深入,具体的内容可以百度。

原文链接:http://blog.csdn.net/u010700335/article/details/42025391





收藏 推荐 打印 | 录入: | 阅读:
本文评论   查看全部评论 (3)
表情: 表情 姓名: 字数
点评:
       
评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款