Python read particular cell value in excel
<p>I have an exel file named "123.csv" that is the output i get when running a PROM functionality, consisting of two columns "case" and "event". I want to modify this output by grouping events based on case. More specifically i want to write a python script that will group events that belong to the same case to be merged in a new cell, no mate what the length of my initial matrix is.Could anyone please give me some idea?
<a href="http://i.stack.imgur.com/ln2QK.png" rel="nofollow">curent and desiret output</a></p>

<pre><code>import csv

with open('123.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in spamreader:
       print ', '.join(row).replace(',',' ').replace('"',' ') 
</code></pre>

<p>this is a part i wrote, but it only reads the file and removes some punctiation</p>

<p>It's easy to do with simple csv &amp; defaultdict (python 3)</p>

<p>Your input is like</p>

<pre><code>case,event
101,A
101,X
101,Y
102,B
102,C
103,Z
</code></pre>

<p>code:</p>

<pre><code>import collections

with open("csv.csv") as f:
    cr = csv.reader(f,delimiter=",")

    d=collections.defaultdict(lambda : list())
    header=next(cr)   # read title
    for r in cr:
        d[r[0]].append(r[1])  # fill dict

with open("csv2.csv","w",newline="") as f:
    cr = csv.writer(f,delimiter=",")
    cr.writerow(header)  # title
    for k,v in d.items():
        cr.writerow([k,",".join(v)])
</code></pre>

<p>output</p>

<pre><code>case,event
103,Z
101,"A,X,Y"
102,"B,C"
</code></pre>

python csv