Adding specific fields from csv

I am trying to add specific values ​​from a CSV file if the user is the same. I can't explain it clearly, so I'll try to show you.

=====================
|E-mail  | M-count |
|a@a.com | 12      |
|b@a.com | 8       |
|a@a.com | 13      |
|c@a.com | 2       |
=====================

      

Then it tries to add everything that belongs to a specific user:

=====================
|E-mail  | Total   |
|a@a.com | 25      |
|b@a.com | 8       |
|c@a.com | 2       |
=====================

      

I have stripped the CSV and added the values ​​I need in the set, but I can't think of a way to add the values ​​I want. Any ideas?

Edit:

This is what my CSV looks like:

p_number,duration,clnup#
5436715524,00:02:26,2
6447654246,00:17:18,5
5996312484,00:01:19,1
5436715524,00:10:12,6

      

I would like to get the total duration and total clnup # for each unique p_number. I'm sorry about the confusion, but the above table was just an example.

+3


source to share


2 answers


You can use OrderedDict

to store names as values ​​and update the counter as you go:

import csv
from collections import OrderedDict

od = OrderedDict()

with open("test.txt") as f:
    r = csv.reader(f)
    head = next(r)
    for name,val in r:
        od.setdefault(name, 0)
        od[name]  += int(val)

print(od)
OrderedDict([('a@a.com', 25), ('b@a.com', 8), ('c@a.com', 2)])

      

To update the original file that you can write to the NamedTemporaryFile, use shutil.move to replace the original after you've written the lines with icons using od.items:

import csv
from collections import OrderedDict
from shutil import move
from tempfile import NamedTemporaryFile
od = OrderedDict()

with open("test.txt") as f, NamedTemporaryFile(dir=".",delete=False) as out:
    r = csv.reader(f)
    wr = csv.writer(out)
    head = next(r)
    wr.writerow(head)
    for name,val in r:
        od.setdefault(name, 0)
        od[name]  += int(val)
    wr.writerows(od.iteritems())


move(out.name,"test.txt")

      



Output:

E-mail,M-count
a@a.com,25
b@a.com,8
c@a.com,2

      

If you don't need ordering, use defaultdict instead:

import csv

from collections import defaultdict
from shutil import move
from tempfile import NamedTemporaryFile
od = defaultdict(int)

with open("test.txt") as f, NamedTemporaryFile(dir=".",delete=False) as out:
    r = csv.reader(f)
    wr = csv.writer(out)
    head = next(r)
    wr.writerow(head)
    for name,val in r:
        od[name]  += int(val)
    wr.writerows(od.iteritems())

      

+1


source


import csv

ifile = open('sample.csv', 'rb')
csv_reader = csv.reader(ifile)

d = {}
for row in csv_reader:
    d[row[0]] = int(row[1]) if d.get(row[0], None) is None else d[row[0]] + int(row[1])
from pprint import pprint
pprint(d)

      



0


source







All Articles