個人筆記,不定期更新
2018-1-6
- 使用Pyinstaller打包Python程序(自定義圖標)
pyinstaller -F -c --icon="iconPATH" programPATH
- psutil常用方法
psutil.cpu_count() # CPU邏輯數量 psutil.cpu_count(logical=False) # CPU物理核心 psutil.cpu_percent(0)# CPU使用率 psutil.virtual_memory()#物理內存信息 psutil.pids() # 所有進程ID
p = psutil.Process(pid) # 獲取指定進程 p.name() # 進程名稱 p.exe() # 進程exe路徑 p.cmdline() # 進程啟動的命令行 p.cpu_times() # 進程使用的CPU時間 p.memory_info() #進程使用的內存 p.terminate() # 結束進程
2018-1-7
-
列表去重
>>> test = ['a','a','b','c','c'] >>> test = list(set(test)) >>> prinrt(test) ['a','b','c']
-
items()
以列表形式返回可遍歷的(鍵, 值) 元組數組testDict = {'谷歌':'Google','百度':'Baidu','聯想':'Lenovo'} print(testDict.items()) for key,values in testDict.items(): print(key,values)
運行結果
dict_items([('谷歌', 'Google'), ('百度', 'Baidu'), ('聯想', 'Lenovo')]) 谷歌 Google 百度 Baidu 聯想 Lenovo
-
tuple = sorted(form.items(), key=lambda e: e[0], reverse=False)
- sorted中的各個參數
- form.items()為迭代的對象
- key用于接收一個函數,實現自定義排序
- reverse=False 為升序排序,反之則為降序
- lambda為匿名函數,e用于接收form.items()的元素作為參數
- e[0]取出key值,為按鍵排序
- e[1]取出value值,為按值排序
綜上,這行代碼的意思是,將字典form按鍵升序排列,返回一個元組tuple
- sorted中的各個參數
2018-1-30
- 文本替換
with open('1.txt','r',encoding='utf-8') as f,open('2.txt','w',encoding='utf-8') as f2:
for i in f:
i = i.replace('old','new')
f2.write(i)
2018-2-12
- 使用pyintaller打包應用出現報錯
UnicodeDecodeError: 'gbk' codec can't decode byte 0xb3 in position 172: illegal multibyte sequence
解決方案:將Python中文文件名修改為英文