csvファイルを平均して1つのファイルとして保存

1171 ワード

import pandas as pd
import glob
import os
import csv
import sys


input_path = './'
output_file = 'tem_avr.csv'

output_header_list = ['index', 'average_tem']

csv_out_file = open(output_file, 'a', newline='')
fwriter = csv.writer(csv_out_file)
fwriter.writerow(output_header_list)

for input_file in glob.glob(os.path.join(input_path, '*.csv')):
    with open(input_file, 'r', newline='') as csv_in_file:
        freader = csv.reader(csv_in_file)
        output_list = []
        output_list.append(os.path.basename(input_file[:11]))
        header = next(freader)
        total_tem = 0.0
        number_of_tem = 0.0
        for row in freader:
            tem_amount = row[6]
            total_tem += float(str(tem_amount).strip().replace(',', ''))

            number_of_tem += 1
        average_tem = '{0:.2f}'.format(((total_tem / number_of_tem)-1000)/10)

        # output_list.append(total_tem)
        output_list.append(average_tem)

        fwriter.writerow(output_list)
        print(output_list)
csv_out_file.close()