I have this dictionary that takes data from a csv file:
def read_results(full_file_path):
csv_dict = {}
with open(full_file_path,'r') as t:
table = t.readlines()[1:]
for line in table:
line = line.replace('\n', '')
line = line.split(',')
line = list(map(float, line))
key = (line[1], line[3])
if key in csv_dict:
csv_dict[key].append((line[4], line[5], line[6]))
else:
csv_dict[key] = [(line[4], line[5], line[6])]
return csv_dict
#that looks like this:
{(1.0, 3.0): [(602.0, 1661.0, 0.0), (945.0, 2164.0, 0.0), (141.0, 954.0, 0.0), (138.0, 913.0, 0.0),....}
but now i need to make use of this dictionary to create a csv of my own that needs to calculate the mean of each value row to its corresponding key couple like this:
c b first finish fix/ext
1 3 744.67 1513.67 0.67
0.8 3 88 858.67 0.67
0.8 1.5 301.5 984.5 0.5
1 1.5 419 844.5 0
and i cant use any outside libraries or modules, what i tried until now :
def Summarize_res(results):
with open('summary_results.csv', 'w', newline='') as f:
header = ['c','b','first','finish','fix/ext']
f.write(str(header))
for line in dict:
first = sum(line[4])/len(line[4])
finish = sum(line[5])/len(line[6])
fix_ext = sum(line[5])/len(line[6])
source https://stackoverflow.com/questions/71541006/python-how-to-create-a-csv-table-using-python-dictionary-without-using-any-mod
Comments
Post a Comment