Python 實際能做什麼?
舉例來說:每週一,一位行政助理要取出收銀機的匯出檔,刪除空白列,再把總額抄進報表。一支約二十行的 Python 程式可以代替她做這件事,每週都以相同方式完成:略過空白列、按產品加總,再寫出報表。在一份測試匯出檔上試跑,結果是「2 個產品已列入報表」。這正是 Python 基礎課程結束時的目標程度。
- 在辦公室節省時間:合併 CSV 檔案、批次重新命名文件、清理匯出資料。
- 通往資料:Python 是資料分析與機器學習的基礎工具。
- 開發應用程式:Python 負責網站與 API 的伺服器端運作。
- 用 AI 來建構:大多數生成式 AI 工具與 AI 代理都可以用 Python 操作。
import csv
totaux = {}
with open("export_caisse.csv", encoding="utf-8") as f:
for ligne in csv.DictReader(f, delimiter=";"):
if not ligne["produit"].strip():
continue # 空白列:略過
montant = float(ligne["montant"].replace(",", "."))
totaux[ligne["produit"]] = totaux.get(ligne["produit"], 0) + montant
with open("rapport_semaine.csv", "w", encoding="utf-8", newline="") as f:
sortie = csv.writer(f, delimiter=";")
sortie.writerow(["produit", "total"])
for produit, total in sorted(totaux.items()):
sortie.writerow([produit, f"{total:.2f}"])
print(len(totaux), "個產品已列入報表")
