根据分组条件连接csv中的列值

2024-06-26 03:02:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个如下所示的csv(注意:Name列中的值不受限制,即不仅仅是ABCDEF):

Name, Type, Text 
ABC, Type A, how
ABC, Type A, are
ABC, Type A, you
ABC, Type B, Your
ABC, Type B, Name?
DEF, Type A, I
DEF, Type A, am
DEF, Type A, good
DEF, Type B, I'm
DEF, Type B, Terminator
... and more 

我想创建另一个csv文件,如下所示(即,基于每个Name列的Type列的组Text列):

Name, Type, Text
ABC, Type A, how are you
ABC, Type B, Your Name?
DEF, Type A, I am good
DEF, Type B, I'm Terminator
..till the end

我正在尝试编写一个python脚本。我的尝试如下:

TypeList = ['Type A','Type B']
with open("../doc1.csv", encoding='utf-8', newline='', mode="r") as myfile:
    
    g = csv.reader(myfile)

    with open("../doc2.csv", encoding='utf-8', newline='', mode="w") as myfile:
        h = csv.writer(myfile)
        h.writerow(["Name","Text"])

        for row in g:
            if TypeList[0] in row[1]:    
               Concatenatedtext[0]= Concatenatedtext[0] + ' ' + row[1]

有人能帮我解决这个烂摊子吗


Tags: csvtextnameyouyourdeftypeam
1条回答
网友
1楼 · 发布于 2024-06-26 03:02:55

将csv行分组在一起是itertools.groupby函数的任务

itertools.groupby接受定义匹配行的键函数,并为找到的每个匹配项发出键(这里是名称和类型)和组(匹配的行)

operator.itemgetter函数可用于创建键函数

import csv
import itertools
import operator

# A function that gets the Name and Type values for each row:
# this is used to group the rows together.
key_func = operator.itemgetter(0, 1)

with open('myfile.csv', newline='') as f:
    reader = csv.reader(f)
    # Skip header row
    next(reader)
    for key, group in itertools.groupby(reader, key=key_func):
        text = ' '.join(cell[2] for cell in group)
        print([key[0], key[1], text])

输出:

['ABC', ' Type A', ' how  are  you']
['ABC', ' Type B', ' Your  Name?']
['DEF', ' Type A', ' I  am  good']
['DEF', ' Type B', " I'm  Terminator"]

相关问题 更多 >