问题描述
鉴于两个单元格包含相同的数据,我正在尝试将多行csv数据合并为一个长行。例如,使用以下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
如果两行在row [0]处共享相同的值,我希望将第二行追加到第一行。因此我的最终产品应如下所示:
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)
对我来说一切正常,但我的scr ipt仅输出以下内容:
Things LOOK ok to me, but my script only outputs the following:
one, two, three
1, 2, 3, ['1', '1', '1']
这显然是错误的,原因有两个。首先,它不会遍历整个csv,其次,附加值被压缩到一个单元格中,而不是单个单元格中。如果有人对如何使其正常工作有任何建议,我将不胜感激!
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!
推荐答案
请改用字典。从您的代码中间开始(假设我已经声明了一个名为my_dict的字典):
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)
这篇关于Python CSV:合并具有相同字段的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!