Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding specific fields from a csv

Tags:

python

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 will try to show you.

=====================
|E-mail  | M-count |
|[email protected] | 12      |
|[email protected] | 8       |
|[email protected] | 13      |
|[email protected] | 2       |
=====================

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

=====================
|E-mail  | Total   |
|[email protected] | 25      |
|[email protected] | 8       |
|[email protected] | 2       |
=====================

I split the CSV and added the values that I need in a set, but I can't think of a way to add the values that I need. 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 the total clnup# for each unique p_number. I am sorry for the confusion but the table above was just an example.

like image 804
user5000054 Avatar asked Mar 08 '26 03:03

user5000054


1 Answers

You can use an OrderedDict storing the names as values and updating the count 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([('[email protected]', 25), ('[email protected]', 8), ('[email protected]', 2)])

To update the original file you can write to a NamedTemporaryFile then use shutil.move to replace the original after you have written the rows with writerows using the 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
[email protected],25
[email protected],8
[email protected],2

If you don't care about order use a 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())
like image 119
Padraic Cunningham Avatar answered Mar 10 '26 18:03

Padraic Cunningham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!