csvファイル(r_sho1~r_sho10)を python3 test.py r_sho{[1-9],10}.csvを実行して読み込み最大値、最小値、平均値を別のcsvファイルとして保存したい
csvファイルを読み込み最大値、最小値、平均値は表示することができ、r_sho10の結果だけdata.csvに保存される。
python
1import sys 2import csv 3 4for fn in sys.argv[1:]: 5 with open(fn, 'r', encoding='Shift_JIS') as file: 6 for line in file: 7 max = 0 8 min = 1e31 9 avg = 0 10 count = 0 11 for num in line.split(','): 12 n = float(num) 13 avg += n 14 if max < n: 15 max = n 16 if min > n: 17 min = n 18 count += 1 19 avg /= count 20 print(','.join([str(max),str(min),str(avg)])) 21 data = ','.join([str(max),str(min),str(avg)]) 22 with open('data.csv', 'wt', encoding='utf-8') as fout: 23 fout.write(data + '\n') 24 25
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/11/02 04:41
2022/11/02 06:35 編集
2022/11/03 00:02