import glob,csv
import pandas as pd
paths = glob.glob('/*.csv')
print(paths)
paths2 = sorted(paths)
print(paths2)
csvfile = paths2[-1]
print(csvfile)
df = pd.read_csv(csvfile,encoding='UTF-8')
print(df)
prize = df['賞金'].sum()
print(f"獲得賞金 {prize}")
race_count = len(df)
print("レース数 " + str(len(df)))
list = df['着順'].tolist()
一着回数 = len([i for i in list if i == 1])
二着回数 = len([i for i in list if i == 2])
三着回数 = len([i for i in list if i == 3])
着外回数 = len([i for i in list if i > 3])
着別度数 = f"{一着回数}-{二着回数}-{三着回数}-{着外回数}"
print("着別度数 " + 着別度数)
list_output = [{"獲得賞金":prize,"着別度数":着別度数}]
# 集計ファイル名作成(集計:aggregate)
filename = csvfile.split(".")[0] + "_agg.csv"
field_name = ['獲得賞金','着別度数']
with open(filename,'w',encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames = field_name)
writer.writeheader()
writer.writerows(list_output)