简体   繁体   English

如何根据列的某种模式对 csv 文件的行进行分组?

[英]How to group the rows of a csv file based on some pattern of a column?

I have the following csv file.我有以下 csv 文件。 I want to group the rows based on the pattern of the 'city' column.我想根据“城市”列的模式对行进行分组。 If the City is X then first three rows will group into a csv file.如果城市是 X,则前三行将分组到一个 csv 文件中。 If the City pattern is YZ then the 4th and 5th rows forms another group.如果城市模式是 YZ 那么第 4 行和第 5 行 forms 另一组。 Likewise, if the city pattern is YAZ then the 6, 7 and 8 rows forms a group.同样,如果城市模式是YAZ那么第6、7、8行forms一组。 Again, if the pattern XX is repeated then 9th and 10th rows forms another group.同样,如果模式 XX 重复,则第 9 行和第 10 行 forms 是另一组。

Sample of the csv file csv 文件的样本

If I understand correctly you are trying to filter the rows of the csv file based on the values of the last column?如果我理解正确,您是在尝试根据最后一列的值过滤 csv 文件的行吗? Here is an example using nested for loops.下面是一个使用嵌套 for 循环的示例。 It might need some adjustments to match exactly what you need though.它可能需要一些调整才能完全符合您的需要。

def filter_csv(csvfile):
    combs = ["X", "XX", "YAZ", "YZ"]
    groups = [[] for _ in combs]
    lines = csvfile.split("\n")
    for line in lines:
        cells = line.split(",")
        for i, comb in enumerate(combs):
            if cells[-1] in comb:
                 groups[i].append(line)            
     return groups

 filter_csv(csvfile)      

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM