1. 程式人生 > >(轉)大資料處理之道(十分鐘學會Python)

(轉)大資料處理之道(十分鐘學會Python)

轉自:http://blog.csdn.net/u010700335/article/details/42025391,如侵刪
(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轉換為一個整數

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 plain copy
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) 綜合應用,檔案讀取,控制檯讀取,時間轉化,編碼轉換
[python] view plain copy
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)中提到的學習網站是非常基礎的人們課程,要是想深入,具體的內容可以百度