Python 具体有什么用?
举个例子:每周一,一位行政助理要取出收银系统导出的数据,删掉空行,再把合计抄进报告。一个二十行左右的 Python 程序就能代她完成这项工作,每周做法完全一致:忽略空行,按产品汇总,写出报告。用一份测试导出文件试运行后,显示“2 个产品已写入报告”。这就是“Python 基础”课程结束时要达到的水平。
- 节省办公时间:合并 CSV 文件、批量重命名文档、清理导出数据。
- 迈向数据领域:Python 是数据分析和机器学习的基础工具。
- 开发应用:Python 运行着网站和 API 的服务器端。
- 借助 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), "个产品已写入报告")
