# [python]python簡易筆記 ###### tags: `Python` ## 網路教學 再見了 pip!最佳 Python 套件管理器——Poetry 完全入門指南 https://blog.kyomind.tw/python-poetry/ ## Dictionary ### check key/value exists in dictionary https://stackoverflow.com/questions/8214932/how-to-check-if-a-value-exists-in-a-dictionary-python ```python= d = { 'a':111, 'b':222, 'c':333 } k = 'a' v = 555 print(k in d.keys()) #=>True print(v in d.values()) #=>False ``` ## 字串 ### 數字轉字串 ```python num = 1 output = '0' + str(num) print(output) #=> 01 ``` ## 使用者輸入 ### 偵測鍵盤輸入 https://stackoverflow.com/questions/24072790/detect-key-press-in-python ```python import keyboard while True: if keyboard.read_key() == "p": print("You pressed p") break ``` ### 按esc退出 https://stackoverflow.com/questions/50733662/how-to-continue-or-exit-the-program-by-pressing-keys ```python! import sys import keyboard a=[1,2,3,4] print("Press Enter to continue or press Esc to exit: ") while True: try: if keyboard.is_pressed('ENTER'): print("you pressed Enter, so printing the list..") print(a) break if keyboard.is_pressed('Esc'): print("\nyou pressed Esc, so exiting...") sys.exit(0) except: break ``` ## file I/O ### 檢查文字檔案是否為空白 https://stackoverflow.com/questions/2507808/how-to-check-whether-a-file-is-empty-or-not ```python! def is_file_empty(file): content = open(file,"r",encoding='UTF-8-sig').read() import re if re.search(r'^\s*$', content): return True ``` ## 編譯 ### 將專案打包成可執行檔(.exe) https://medium.com/pyladies-taiwan/python-%E5%B0%87python%E6%89%93%E5%8C%85%E6%88%90exe%E6%AA%94-32a4bacbe351 ```python pyinstaller -F .\ur_file_name.py ```