[Solved] Create python dictionary from TXT file – value aggregation


No need to bother with pandas. Text files are iterable. Just open it, operate on the line (string) and fill a dictionnary.

file = "font.txt"

with open(file, "r") as f:
    dic = dict()
    for line in f:
        x = line.strip("\n").split(" ")

        key = int(x[0].strip("px"))
        value = int(x[1])

        if key not in dic.keys():
            dic[key] = [value]
        else:
            dic[key].append(value)

Output:

{22: [31, 43], 11: [326, 291], 18: [8], 13: [41]}

Then it’s simple math:

total_number_of_character = sum([sum(x) for x in dic.values()])
percentage = percentage = {key:sum(value)/total_number_of_character*100 for (key, value) in dic.items()}

Output:

{22: 10.0,
 11: 83.37837837837839,
 18: 1.0810810810810811,
 13: 5.540540540540541}

2

solved Create python dictionary from TXT file – value aggregation