python怎么读取excel中的某个单元格-Python在excel中读取特定单元格值

1586010002-jmsa.png

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?

curent and desiret output

import csv

with open('123.csv', 'rb') as csvfile:

spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')

for row in spamreader:

print ', '.join(row).replace(',',' ').replace('"',' ')

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

解决方案

It's easy to do with simple csv & defaultdict (python 3)

Your input is like

case,event

101,A

101,X

101,Y

102,B

102,C

103,Z

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)])

output

case,event

103,Z

101,"A,X,Y"

102,"B,C"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值