大數據處理之道(十分鍾學會Python)


一: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 = 
  4. while not  done: 
  5.         aLine = file.readline() 
  6.         if(aLine != ''): 
  7.             print aLine, 
  8.         else: 
  9.             done = 
  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 = 
  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 = 
  29. b = 
  30. c = a**b   
  31. print "Line 6 - Value of c is ", c 
  32.  
  33. a = 10 
  34. b = 
  35. c = a//b   
  36. print "Line 7 - Value of c is ", c 
  37. # for repeat its 
  38. list = [2, 4, 6, 8] 
  39. sum = 
  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 = 
  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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM