python - Comparing file content -
python - Comparing file content -
i have script generates several output files (ex: out0.txt out250.txt) , want able compare specific value of them , output top 10 highest, specific, values in of them.
for example, in each of these output files there multiple lines containing various data, lines interested ones contain, on own line, match statistics. here's illustration extract 1 of files.
.... score matches: 592 (52.3%) #the 52.3 part of 592 portion ref: 1 gt...... query: 340 matches: 584 (54.5%) #and 54.3
specifically, interested in percentage part display top 10 highest percentages in of files.
i have split files before/taken in specific data, typically relied on line number. unfortunately location of these 'matches' lines bit more irregular , not every 3rd line or so.
should i seek have programme numbers next % symbol, considering it's part of file's output info provides that?
in short, how extract value of percentage portions files, amidst other string output, compare , output 10 highest?
thank you,
import re def get_values_from_file(filename): f = open(filename) winpat = re.compile("([\d\.]+)\%") values = [] line in f.readlines(): if line.find("matches") >=0: percn = float(winpat.findall(line)[0]) values.append(percn) homecoming values all_values = [] filename in ["out0.txt", "out1.txt"]: values = get_values_from_file(filename) all_values += values all_values.sort() all_values.reverse() print(all_values[0:10])
python parsing file-io
Comments
Post a Comment