python 學習筆記
1 import :
(1)import module
(2)from module import argv
(3)from module import *
2 item :
item :把字典中的每一對 key 和 value 組成一個元組,並返回 (返回的是由字典中某一個元素的 key 和 vaule )
3 split :
split: python 中有兩個split函數:
split():拆分字符串。通過指定的分隔符對字符串進行切片,並返回分割後的字符串列表(list)
os.path.split():按照路徑將文件名和路徑分割開。
(1) split()函數:
str . split ( 分隔符, 分割的次數) [ 選擇哪一部分 ]
例子: 分割字符串
str = “www.baidu.com.cn!!!!!”
print ( str.split ( ' . ' ))
[ 'www', 'baidu' , 'com' , 'cn!!!!' ]
表示以 . 爲分割符對str 進行分割
print ( str .split ( ' . ' ), 1 )
['www' , 'baidu.com.cn!!!!' ]
只分割1次, 其餘的不分割 輸出.
print ( str . split ( ' . ' ), 2 ) [ 0]
[ ' baidu']
分割兩次, 取第幾個片段
str1, str2 ,str3 = str . split ( ' . ', 2 )
print (str1) ===== www
print ( str2) ===== baidu
print (str3) ====== com.cn!!!!
(2) 分離文件名和路徑:
import os
print ( os. path. split ( ' /var / panoview / modules / setup / time.js ' ))
( ' /var /panoview / modules / setup ' , ' time.js ' )
例如: str = " hello boy < [ www.daidu.com ]> bye bye"
print ( str. split ( " [" ) [ 1] . split ( " ] ") [ 0] )
www.daidu.com
4 strip :
申明: s 爲字符串 , dest 爲需要刪除的 字符序列
s.strip ( dest ): 刪除整個字符串開頭 和結尾 處的 ,位於 字符序列 中的 字符
s.lstrip (dest ): 只刪除開頭
s.rstrip (dest) :只刪除末尾
注意:
當dest 爲空時, 默認刪除空白符 包括 : ' \n ' , ' ' , ' \t '
例子:
str = " hello word"
str. strip ()
hello word
str = "##!!!##hello word \n 123"
str. strip ( "#!\n 123")
hello word
str. lstrip ("#!")
hello word \n 123
str. strip ("\n 123")
##!!!##hello word
5 encode :
encode 編碼方式:
str . encode ( enconding = "UTF - 8 ", errors = " strict ")
enconding ---- 需要使用的編碼方式
errors -------- 設置不同錯誤的處理方式
6 input raw_input :
這兩個函數都能接收字符串,只不過 raw_inout ( )直接讀取控制檯的輸入 ( 輸入任何數據類型,都當作字符串 ) . 而 input ( ) , 它希望能夠得到一個合法的 " 表達式" , 它會對輸入的 "表達式" 進行計算處理,並返回結果 .
input : 參數當作 "表達式" , 自動檢查數據類型, 自動進行計算處理,並返回表達式的結果
raw_input: 任何輸入的參數都當作字符串處理.
注:
實質上input( )還是在使用了 raw_input ( ) 函數之後, 再調用 eval ( ) 函數.
7 type
type ( ): 接收一個對象作爲參考, 並返回對象的 類型.
8 splitlines
splitlines ( ): 按照行 ( ' \ n ' , ' \r ' , ' \r \ n ' ) 分割, 返回一個包含各行所有字符的列表, 如果參數 keepends 爲 false , 不包含換行符, 如果爲true , 則保留換行符.
9 startwith
startwith ( ) : 用於檢查字符串是否以 定義的 字符串開始, 如果是 返回 true , 否則返回 false.
str. startwith ( dest_str , beg = 0, end = len (str) )
dest_str : 表示需要檢測的字符串
beg: 設置從哪裏開始檢測, 起始位置
end : 結束位置
10 eval
eval ( ) :