You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sledge/runtime/data/get_average.py

31 lines
903 B

import sys
def calculate_average(input_file, column_index):
total = 0
count = 0
with open(input_file, 'r') as f:
for line in f:
columns = line.strip().split(',')
if len(columns) > column_index:
try:
value = float(columns[column_index])
total += value
count += 1
except ValueError:
print(f"error value: {columns[column_index]}")
if count > 0:
average = total / count
print(f"list {column_index + 1} average: {average}")
else:
print("no value")
if __name__ == "__main__":
if len(sys.argv) != 3:
print(" python calculate_average.py input_file column_index")
else:
input_file = sys.argv[1]
column_index = int(sys.argv[2]) - 1
calculate_average(input_file, column_index)