Home > Blockchain >  How to sum specific values from two different txt files in python
How to sum specific values from two different txt files in python

Time:02-05

I have 2 txt files with names and scores. For example:

File 1          File 2           Desired Output
Name     Score  Name     Score   Name     Score
Michael  20     Michael  30      Michael  50
Adrian   40     Adrian   50      Adrian   90
Jane     60                      Jane     60

I want to sum scores with same names and print them. I tried to pair names and scores in two different dictionaries and after that merge the dictionaries. However, I can't keep same names with different scores. So, I'm stuck here. I've written something like following :

d1=dict()
d2=dict()
with open('data1.txt', "r") as f:
    test = [i for line in f for i in line.split()]
    i = 0
    while i < len(test) - 1:
        d1[test[i]] = test[i   1]
        i  = 2
    del d1['Name']

with open('data2.txt', "r") as f:
    test = [i for line in f for i in line.split()]
    i = 0
    while i < len(test) - 1:
        d2[test[i]] = test[i   1]
        i  = 2
    del d2['Name']
z = dict(d2.items() | d1.items())

CodePudding user response:

Using a dictionary comprehension should get you what you are after. I have assumed the contents of the files are:

File1.txt:

Name     Score
Michael  20
Adrian   40
Jane     60

File2.txt:

Name     Score
Michael  30
Adrian   50

Then you can get a total as:

with open("file1.txt", "r") as file_in:
    next(file_in) # skip header
    file1_data = dict(row.split() for row in file_in if row)

with open("file2.txt", "r") as file_in:
    next(file_in) # skip header
    file2_data = dict(row.split() for row in file_in if row)

result = {
    key: int(file1_data.get(key, 0))   int(file2_data.get(key, 0))
    for key
    in set(file1_data).union(file2_data) # could also use file1_data.keys()
}

print(result)

This should give you a result like:

{'Michael': 50, 'Jane': 60, 'Adrian': 90}

CodePudding user response:

Use defaultdict

from collections import defaultdict


name_scores = defaultdict(int)
files = ('data1.txt', 'data2.txt')
for file in files:
    with open(file, 'r') as f:
        for name, score in f.split():
            name_scores[name]  = int(score)

edit: You'll probably have to skip any header line and maybe clean up trailing white spaces, but the gist of it is above.

  •  Tags:  
  • Related