0

I have this file

10, 44.66.44.55 , AD, AC , 112
10, 44.66.54.55 , AD, AC , 112
10, 44.66.44.55 , AD, AC , 112
50, 44.66.64.55 , AD, AC , 112
10, 44.66.54.55 , AD, AC , 112
10, 44.66.44.55 , AD, AC , 112

I want to add the column1 with same ip address. i want the output like

30, 44.66.44.55 , AD, AC , 112
20, 44.66.54.55 , AD, AC , 112
50, 44.66.64.55 , AD, AC , 112

I want to do in python

I tried

import re
import collections

a = collections.Counter()
with open("temp.txt", "r") as f:
   for line in f.readlines():
         list = line.split()
     a[list[1]] += int(list[0])
         print list[1]
3
  • Try using a collections.Counter. Commented Jan 30, 2013 at 3:28
  • @nneonneo thanks , i forgot that , i will try that Commented Jan 30, 2013 at 3:31
  • @nneonneo , i tried the above and it gave the error , invalid literal Commented Jan 30, 2013 at 3:37

4 Answers 4

1

You may use the itertools.groupby solution, which is ideal in this case

>>> with open("test.csv") as fin:
    grouped_lines = groupby(sorted((e.split(',') for e in fin), key = itemgetter(1)), key = itemgetter(1))


>>> for k, v in grouped_lines:
    lines = list(v)
    lines[0][0] = sum(int(e[0]) for e in lines)
    print lines[0]


[30, ' 44.66.44.55 ', ' AD', ' AC ', ' 112\n']
[20, ' 44.66.54.55 ', ' AD', ' AC ', ' 112\n']
[50, ' 44.66.64.55 ', ' AD', ' AC ', ' 112\n']
Sign up to request clarification or add additional context in comments.

Comments

1

You need to split on , not on white spaces

try this

list = line.split(',')

Comments

0

There is a collections.Counter module. It returns a dictionary of of {'word': numberTimes} http://docs.python.org/2/library/collections.html

Comments

0

Although @Abhijit's answer is shorter,
Try this, it also works.

After processing the file data, I store the data in a dictionary. This data is then manipulated when needed.
Your data is the values in the dict.

all_ips = {}

f = open('Test2.txt')
lines = f.readlines()
f.close()

for line in lines:
    ip = line.split(',')[1]

    props = line.split(',')
    props[0] = int(props[0])

    if ip not in all_ips:
        all_ips[ip] = props
    else:
        all_ips[ip][0] += props[0]

for ip in all_ips:
    print all_ips[ip]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.