# Ex1 題目: 1. 我想collect data, -- 點collect,先collect 1個 2. 者係例如a1呢個item, -- 點search,先search 1個,再search多個 -- 考慮返回值結果作處理 3. 係1-8呢幾個xls(O_data)入面, total 有幾多件 -- 將1. 想像成一個功能,重複1. 的功能 ## 檔案全部放在`o_data`文件夾中 > o_data/1.xlsx > o_data/3.xlsx > ... > ![Img](https://cdn.jsdelivr.net/gh/mhk00123/my-img/2023/202302211238657.png) ```python= # import Pandas套件、OS套件(用作判斷.xlsx) import pandas as pd from os import listdir import os # 找出目錄中全部.xlsx文件 my_path = "o_data" loc_target = [] # 設置空陣列loc_target用作存放.xlsx文件名 for root, dirs, files in os.walk(my_path): # (迴圈)在my_path中找尋->會show所有文件名(files) for i in files: if(os.path.splitext(i)[1] == '.xlsx'): # 在每個files中判斷副檔名是否包含.xlsx loc_target.append(os.path.join(root,i)) # 如果是,則在loc_target加入該文件名 # Set Target target_no = "a0" # 設置統計參數 target_amount = 0 target_price = 0 target_sum = 0 # 讀取所有.xlsx文件,分別進行累加 for loc in loc_target: df = pd.read_excel(loc,skiprows=7,usecols="B:F") # Search Target target = df[df["編號"]==target_no].reset_index() result_length = len(target.index) for i in range(0,result_length): target_amount = target_amount + target.at[i,"數量"] target_price = target_price + target.at[i,"單價"] target_sum = target_sum + target.at[i,"金額"] # Show Result print(target_amount) print(target_price) print(target_sum) ```