0

For each line in input.txt , write a new line in the new text file output.txt that computes the answer to some operation on a li

I have to make a output.txt file based on a input.txt The input input.txt reads min:1,2,3,5,6 max:1,2,3,5,6 avg:1,2,3,5,6 P90:1,2,3,4,5,6,7,8,9,10 sum:1,2,3,5,6 min:1,5,6,14,24 max:2,3,9 P70:1,2,3 output file should read The min of [1, 2, 3, 5, 6] is 1. The max of [1, 2, 3, 5, 6] is 6. etc. I cant seem to make a few lists , each on a new line. please help

19th Feb 2017, 4:32 PM
Jurie Spies
Jurie Spies - avatar
2 Respostas
0
# to help you on your way with one option try this. # I leave it to you to adapt it to the case of avg # and experiment with writing the processed results to a file. # The python documentation may be overwhelming # if you are just starting to code # https://docs.python.org/3/library/functions.html # but it is a good source of information once you get familiar with it # the example works with the data in the format you have shown. processed = [] with open('input.txt','r') as my_file: for line in my_file: pre = line.strip().split(':') post = [int(x) for x in pre[1].split(',')] if pre[0] in ['min','max','sum']: calc = eval('{0}({1})'.format(pre[0],post)) processed.append('The {0} of {1} is {2}'.format(pre[0],post,calc)) for item in processed: print(item)
19th Feb 2017, 8:10 PM
richard
0
Thanks guys.Richard your answer worked 100% ! would you mind explaining to me how does it work ? I'm still new to programming.
20th Feb 2017, 7:45 PM
Jurie Spies
Jurie Spies - avatar