我是 python 新手,我正在邊做邊學。
目前,我的代碼運行速度很慢,每次運行它似乎需要的時間越來越長。
這個想法是將員工串列下載為 CSV,然后通過在特定頁面上運行它來檢查每個員工 ID 的位置,然后將其寫入 excel 檔案。
我們每天有大約 600 名員工在現場,我需要找到他們的位置并每 2-4 分鐘重繪 一次。
編輯:
為了讓大家更好地理解,我有一個 CSV 檔案 ( TOT.CSV ),其中包含員工 ID、姓名和我在現場擁有的員工的其他資訊。
為了獲取他們的位置,我需要通過https://guided-coaching-dub.corp.amazon.com/api/employee-location-svc/GetLastSeenLocationOfEmployee?employeeId= 1 by 1運行該 CSV 檔案中的每個員工 ID同時將其寫入另一個 CSV 檔案( Location.csv )。現在,它在大約 10 分鐘內完成,我想了解我這樣做的方式是否是最好的方式,或者是否還有其他可以嘗試的方法。
我的代碼如下所示:
# GET EMPLOYEE ID FROM THE CSV
data = read_csv("Z:\\_Tracker\\Dump\\attendance\\TOT.csv")
# converting column data to list
TOT_employeeID = data['Employee ID'].tolist()
# Clean the Location Sheet
with open("Z:\\_Tracker\\Dump\\attendance\\Location.csv", "w") as f:
pass
print("Previous Location data cleared ... ")
# go through EACH employee ID to find out location
for x in TOT_employeeID:
driver.get(
"https://guided-coaching-dub.corp.amazon.com/api/employee-location-svc/GetLastSeenLocationOfEmployee?employeeId=" x)
print("Getting Location data for EmployeeID: " x)
locData = driver.find_element(By.TAG_NAME, 'body').text
aaData = str(locData)
realLoc = aaData.split('"')
# write to excel
with open("Z:\\_Tracker\\Dump\\attendance\\Location.csv",
"a") as f:
writer = csv.writer(f)
writer.writerow(realLoc)
time.sleep(5)
print("Employee Location data downloaded...")
有沒有辦法可以更快地做到這一點?
先感謝您!
問候,亞歷克斯
uj5u.com熱心網友回復:
像這樣的東西。
import concurrent.futures
def process_data(data: pd.DataFrame) -> None:
associates = data['Employee ID'].unique()
with concurrent.futures.ProcessPoolExecutor() as executer:
executer.map(get_location, associates)
def get_location(associate: str) -> None:
driver.get(
"https://guided-coaching-dub.corp.amazon.com/api/employee-location-svc/GetLastSeenLocationOfEmployee?"
f"employeeId={associate}")
print(f"Getting Location data for EmployeeID: {associate}")
realLoc = str(driver.find_element(By.TAG_NAME, 'body').text).split('"')
with open("Z:\\_Tracker\\Dump\\attendance\\Location.csv", "a") as f:
writer = csv.writer(f)
writer.writerow(realLoc)
if __name__ == "__main__":
data = read_csv("Z:\\_Tracker\\Dump\\attendance\\TOT.csv")
process_data(data)
uj5u.com熱心網友回復:
您可以嘗試將讀取資訊和將資訊寫入 CSV 檔案的步驟分開,如下所示:
# Get Employee Location Information
# Create list for employee information, to be used below
employee_Locations = []
for x in TOT_employeeID:
driver.get("https://guided-coaching-dub.corp.amazon.com/api/employee-location-svc/GetLastSeenLocationOfEmployee?employeeId=" x)
print("Getting Location data for EmployeeID: " x)
locData = driver.find_element(By.TAG_NAME, 'body').text
aaData = str(locData)
realLoc = [aaData.split('"')]
employee_Locations.extend(realLoc)
# Write to excel - Try this as a separate step
with open("Z:\\_Tracker\\Dump\\attendance\\Location.csv","a") as f:
writer = csv.writer(f, delimiter='\n')
writer.writerow(employee_Locations)
print("Employee Location data downloaded...")
通過先收集所有資訊,然后寫入 CSV 檔案,您可能會看到一些性能提升
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/515562.html
標籤:Python熊猫硒CSV
