python csv合并单元格_Python CSV:合并具有相同字段的行

I am attempting to merge several rows of csv data into one long row, given two cells contain the same data. For instance, take the following csv:

one, two, three

1, 2, 3

4, 5, 6

7, 8, 9

1, 1, 1

4, 4, 4

If two rows share the same value at row[0], I want the second row appended to the first. So my end product should look like this:

one, two, three

1, 2, 3, 1, 1, 1

4, 5, 6, 4, 4, 4

7, 8, 9

Here is my attempt so far:

import csv

uniqueNum = []

uniqueMaster = []

count = -1

with open("Test.csv", "rb") as source:

reader = csv.reader(source)

header = next(reader)

for row in reader:

if row[0] not in uniqueNum:

uniqueMaster.append(row)

uniqueNum.append(row[0])

count = count + 1

for row in reader:

if row[0] in uniqueNum:

uniqueMaster[count].append(row)

with open("holding.csv","wb") as result:

writer = csv.writer(result)

writer.writerow(header)

for row in uniqueMaster:

writer.writerow(row)

Things LOOK ok to me, but my script only outputs the following:

one, two, three

1, 2, 3, ['1', '1', '1']

This is obviously wrong for two reasons. First, it doesn't iterate through the entire csv, and second, the appended values are being squeezed into one cell, rather than individual cells. If anyone had any advice on getting this to work right I'd highly appreciate it!

解决方案

Use a dictionary instead. Starting from the middle of your code(assume I have declared a dict called my_dict):

for row in reader:

if row[0] in my_dict.keys():

my_dict[row[0]].extend(row)

else:

my_dict[row[0]]=row

#...now we are at the bottom of your code, writing to the csv

for v in my_dict.values():

writer.writerow(v)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值