File: LP6E/AppendixB/Part7/Self-Study-Demos/summer1.py

# Sum columns in a text file separated by commas

filename = 'data.txt'    # Edit me for others
sums = {}

for line in open(filename):
    cols = line.split(',')
    nums = [int(col) for col in cols]
    for (ix, num) in enumerate(nums):
        sums[ix] = sums.get(ix, 0) + num

for key in sorted(sums):
    print(key, '=', sums[key])



[Home page] Books Code Blog Python Author Train Find ©M.Lutz