標籤:code min 相對 es2017 北京 apc 環境變數 res nes
一、兩個模組(sys和os)
1 #!/usr/bin/env python 2 # _*_ coding: UTF-8 _*_ 3 # Author:taoke 4 import sys 5 print(sys.path)#列印環境變數 6 print(sys.argv[0])#當前檔案相對路徑,sys.argv是一個列表,第一個元素為程式本身的相對路徑,之後的為程式運行是的輸入參數 7 8 import os 9 #cmd_res= os.system("dir")#執行命令不儲存結果10 cmd_res = os.popen("dir").read()#儲存命令執行的結果並返回儲存地址11 print("-->",cmd_res)12 os.mkdir("new_dir")#建立一個目錄
sys和os兩個模組的簡易使用
import
現在目前的目錄下尋找模組,在環境變數中尋找模組
存放第三方模組的路徑 C:\Python36-32\Lib\site-packages
二、python中string與bytes之間的轉換
1 #!/usr/bin/env python2 # _*_ coding: UTF-8 _*_3 # Author:taoke4 str = "我愛北京天安門"5 str_endode = str.encode("utf-8")6 str_endode_decode = str_endode.decode("utf-8")7 print(str,type(str))8 print(str_endode,type(str_endode))9 print(str_endode_decode,type(str_endode_decode))
運行結果:我愛北京天安門 <class ‘str‘>b‘\xe6\x88\x91\xe7\x88\xb1\xe5\x8c\x97\xe4\xba\xac\xe5\xa4\xa9\xe5\xae\x89\xe9\x97\xa8‘ <class ‘bytes‘>我愛北京天安門 <class ‘str‘>
三、列表(List)
1 #!/usr/bin/env python 2 # _*_ coding: UTF-8 _*_ 3 # Author:taoke 4 names = ["xiaoming","xiaohong","xiaohei","xiaoxiao"] 5 6 print(names) 7 print(names[0],names[2]) 8 print(names[1:3])#顧頭不顧尾,切片 9 10 names.append("xiaobingbing")11 print(names)12 names.insert(1,"renma")13 print(names)
List中的淺copy和深copy
#!/usr/bin/env python# _*_ coding: UTF-8 _*_# Author:taokeimport copynames = ["xiaoming","xiaohong",["Jack","Toms"],"xiaohei","xiaoxiao"]names2 = names.copy()#淺copynames3 = copy.copy(names)#淺copynames4 = copy.deepcopy(names)#深copynames[2][0] = "JACK"print(names)print(names2)print(names3)print(names4)
運行結果:
[‘xiaoming‘, ‘xiaohong‘, [‘JACK‘, ‘Toms‘], ‘xiaohei‘, ‘xiaoxiao‘][‘xiaoming‘, ‘xiaohong‘, [‘JACK‘, ‘Toms‘], ‘xiaohei‘, ‘xiaoxiao‘][‘xiaoming‘, ‘xiaohong‘, [‘JACK‘, ‘Toms‘], ‘xiaohei‘, ‘xiaoxiao‘][‘xiaoming‘, ‘xiaohong‘, [‘Jack‘, ‘Toms‘], ‘xiaohei‘, ‘xiaoxiao‘]
四、tuple(元組)
不可以更改的列表,只能查。
五、string(字串方法)
- str.rjust:靠右對齊
- str.ljust:靠左對齊
- str.center:中間對齊
- str.zfill:預設的方式
- str.find:字串尋找,沒有返回-1
- str.index:尋找字串位置,沒有返回錯誤
- str.rfind:從右開始尋找
- str.rindex:同上
- str.count:統計字串出現的次數
- str.replace:字串替換
- str.strip:去除字串開頭末尾的空格
- str.lstrip:去除左邊空格
- str.rstrip:去除右邊空格
- str.expandtabs:把字串裡的table換成等長的空格
- str.lower:
- str.upper:
- str.swapcase:將字串字元大小寫反轉
- str.capitalize:字串首字元大寫
- str.title:字串中首字母大寫
- str.split:字串拆分成列表
- str.splitlines:將字串中按行拆分放到列表中
- ‘-‘.join(strList):用‘-’將列表strList串連成字串
- str.startswith:測試字串是否是以指定字元開頭的
- str.endswith:測試字串是否是以指定字元結尾的
- str.isalum:判斷字串是否全是字元或數字並至少有一個字元
- str.isalpha:判斷字串是否全是字母
- str.isdigit:判斷字串是否全是數字
- str.isspace:判斷字串是否含有空格
- str.islower:判斷字串是否全是小寫
- str.isupper:判斷字串是否全是大寫
- str.istitle:判斷首字母是否是大寫
- import string
- string.atoi("123",base=10/8/16):轉換字串到int類型的數字
- string.atol:轉換字串到長整形數字
- string.atof:轉換字串到浮點型
python學習記錄2