0
1st Text file format . 
cake,60
cake,30
tart,50
bread,89  

2nd Text file format . 
cake,10
cake,10
tart,10
bread,10  

Code I have tried.

from collections import defaultdict
answer = defaultdict(int)
recordNum = int(input("Which txt files do you want to read from "))
count = 1
counter = 0
counst = 1
countesr = 0
while recordNum > counter:
  with open('txt'+str(count)+'.txt', 'r') as f:
      for line in f:
          k, v = line.strip().split(',')
          answer[k.strip()] += int(v.strip())
          count = count+1
          counter = counter+1
print(answer)

The problem.

I want the dictionary to be {'cake': '110', 'tart': '60', 'bread': '99'}

but it prints like this  {'cake': '30', 'tart': '50', 'bread': '89'}

Instead of the "cake" value adding with the other cake values from txt file one and two it gets replaced with the latest value. How would I solve this issue. Also i tried to make it so if I write 3, it would open and add from 3 txt files, named, txt1.txt, txt2.txt and txt3.txt

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
  • 1
    Can't reproduce this. It works correctly for me. However it gives 110 cakes (30+60+10+10) and I hardcoded the filenames. – MSeifert Aug 21 '17 at 21:07
  • What do you mean by hard coding the file names?? –  Aug 21 '17 at 21:08
  • Well, I don't like `input` so I just created two files and just iterated over them instead of your `while recordNum > counter:`. – MSeifert Aug 21 '17 at 21:09
  • Please do not vandalize your posts. Once you've posted a question, it belongs to the Stack Overflow community at large (under the CC-by-SA license). If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request?](https://meta.stackoverflow.com/questions/323395/what-is-the-proper-route-for-a-dissociation-request) – DSM Aug 25 '17 at 00:20

2 Answers2

1

The problem is that your 2nd file doesnt get read:

Which txt files do you want to read from 2
defaultdict(<class 'int'>, {})
defaultdict(<class 'int'>, {'cake': 60})
defaultdict(<class 'int'>, {'cake': 90})
defaultdict(<class 'int'>, {'tart': 50, 'cake': 90})
defaultdict(<class 'int'>, {'tart': 50, 'bread': 89, 'cake': 90})
>> terminating

You could make these edits to read all the files (Note: this assumes your text files are named txt1.txt, txt2.txt, txt3.txt and so on..):

from collections import defaultdict
answer = defaultdict(int)
number_of_records = int(input("How many text files do you want to read?"))
for i in range(1, number_of_records+1):
    with open('txt{}.txt'.format(i), 'r') as file:
        for line in file:
            k, v = line.strip().split(',')
            answer[k] += int(v)
print(answer)


How many text files do you want to read?
>> 2
defaultdict(<class 'int'>, {'bread': 99, 'tart': 60, 'cake': 110})
>> terminating
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
0

Don't know if the code is pythonic way, but works for me and is hardcoded.

x={}
y={}
with open("a.txt") as file:
    for i in file:
        (key, val) = i.split(',')
        if key in x.keys(): x[key]=x[key]+int(val.rstrip())
        else: x[key] = int(val.rstrip())

with open("b.txt") as file:
    for i in file:
        (key, val) = i.split(',')
        if key in y.keys(): y[key]=y[key]+int(val.rstrip())
        else: y[key] = int(val.rstrip())

print { k: x.get(k, 0) + y.get(k, 0) for k in set(x) | set(y) }

This may help you : Merge and sum of two dictionaries

Monish K Nair
  • 136
  • 1
  • 13