python 學習筆記
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‘]
分割兩次, 取右邊部分 : 0 表示取 左邊分割開來的字符串 1 表示 右邊的
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 :
本文出自 “12690807” 博客,謝絕轉載!
python 學習筆記