分類彙整:python

python qt icon

#https://www.byhy.net/tut/py/gui/qt_04/ from PySide2.QtGui import QIcon app = QApplication([]) #import icon app.setWindowIcon(QIcon(‘logo.png’)) #png to ico https://www.zamzar.com/convert/png-to-ico/ https://www.easyicon.net/convert/

發表於 python | 發表迴響

python read readline readlines 差別

read() : 讀取整個文件 程式範例: with open(‘test.txt’) as f: test_data = f.read() print( test_data ) 會讀取整個文件 readline() : 讀取下一行 程式範例: with open(‘test.txt’) as f: test_data = f.readline() print( test_data ) 讀取一行 readlines() : 讀取整個文件,遍歷 程式範例: with open(‘test.txt’) as f: test_data = … 繼續閱讀 →

發表於 python | 發表迴響

python 裝飾器

def 裝飾器 (callback): def 內部function(): 裝飾器程式碼 callback() return 內部function 使用裝飾器 @裝飾器 def function(): #function 程式碼 function() #呼叫帶裝飾器函式 #範例 import time def sayLocal(func): def wrapper(*args,**kargs): curTime = func(*args,**kargs) return f’当地时间: {curTime}’ return wrapper @sayLocal def getXXXTimeFormat1(name): curTime = time.strftime(‘%Y-%m-%d %H:%M:%S’,time.localtime()) return … 繼續閱讀 →

發表於 python | 發表迴響

python search txt 方式

https://www.delftstack.com/zh-tw/howto/python/python-find-string-in-file/#%e5%9c%a8-python-%e4%b8%ad%e4%bd%bf%e7%94%a8%e6%aa%94%e6%a1%88-readlines-%e6%96%b9%e6%b3%95%e6%9f%a5%e8%a9%a2%e6%aa%94%e6%a1%88%e4%b8%ad%e7%9a%84%e5%ad%97%e4%b8%b2 https://github.com/dokelung/Python-QA/blob/master/questions/file/Python%E5%A6%82%E4%BD%95%E5%AF%A6%E7%8F%BE%E4%B8%A6%E8%A1%8C%E6%9F%A5%E6%89%BE%E9%97%9C%E9%8D%B5%E5%AD%97%E6%89%80%E5%9C%A8%E7%9A%84%E8%A1%8C.md 建議使用下列 不要用 readline() with open(‘XXX’, ‘r’) as f: for line in f: if ‘txt’ in line: # do something…

發表於 python | 發表迴響

python 路徑 path 加入問題

#之前對變數加入問題,查到的解決方案 from pathlib import Path nday = ’201201020.log’ #pathlib.Path() palib = Path(‘c:’,’/’,’Users’, nday) #print ( Path(‘c:’,’/’,’Users’) ) print ( palib ) #os.path() import os ospath = ( os.path.join(‘c:’, os.sep, ‘Users’, nday) ) print ( ospath ) 或者是 flogfile = "D:\\Backup\\hsbat\\log\\" … 繼續閱讀 →

發表於 python | 發表迴響

python mixin

class PrettyMinin(): def dump(self): from pprint import pprint pprint(vars(self)) Mixmin 類別,用於紀錄,印出屬性

發表於 python | 發表迴響

python-Decorator

https://www.youtube.com/watch?v=qc8hsxAK270&list=PL-g0fdC5RMboYEyt6QS2iLb_1m7QcgfHk&index=34 定義裝飾器 def 定義裝飾器(回呼函式): def 內部函式(): #裝飾器函式內碼 回呼函式名稱() return 內部函式名稱 使用裝飾器 @裝飾器名稱 def 函式名稱(): #函式程式碼 函式名稱() #呼叫帶有裝飾器名稱 範例: def testDecorator(callback): def innerFunc(): callback() return innerFunc @testDecorator def decoratedFunc(): print("普通函式") defcoratedFunc() =============== #定義裝飾器 def myDeco(cb): def run(): print("1.裝飾器中的程式碼") cb(5) #這回呼函式,其實是被裝式的函式 test … 繼續閱讀 →

發表於 python | 發表迴響

Python – call back

#callback def test(arg): #print(arg) arg("hello") # function call #arg(hello) to handle(data) #定義一個回呼函式 def handle(data): print(data) test(handle) def add(n1, n2, cb): cb(n1+n2) def handle1(result): print("結果是: ", result ) def handle2(result): print("English Result of Add is: ", result ) add(3, 4, handle1) … 繼續閱讀 →

發表於 python | 發表迴響

python selenium-3

標籤ID driver.find_element(By.ID,"條件") 標籤屬性搜尋 CSS driver.find_element(By.CSS_SELECTOR,"[屬性名稱=屬性的值]") 模擬user input element=driver.find_elemnet(search欄位,"條件") element.send_keys("input key) 模擬 user inpurt key element.send_keys(Keys.ENTER) ======================================= #https://www.youtube.com/watch?v=tR558fQAHz8 #爬 leetcode #https://leetcode.com/accounts/login/ #pip install selenium from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options import … 繼續閱讀 →

發表於 python | 發表迴響

python selenium-2

瀏覽器中執行程式 driver.execute_script("JavaScript Code") 捲動視窗到底部 dirver.execute_script("window.scrollTo(0,document.body.scrollHeight);)") 等待一陣子在捲動 imprt time time.sleep("wait s") ========================== #https://www.youtube.com/watch?v=ZOxIpMTgaXY #https://www.linkedin.com/jobs/search?trk=guest_homepage-basic_guest_nav_menu_jobs&position=1&pageNum=0 #爬linkin #pip install selenium from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options import time #wating html loading #chrom driver path https://chromedriver.chromium.org/downloads options=Options() options.chrome_executable_path="G:\我的雲端硬碟\PY\練習\chromdriver" … 繼續閱讀 →

發表於 python | 發表迴響