I have the following csv file. 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. If the City pattern is Y Z then the 4th and 5th rows forms another group. Likewise, if the city pattern is Y A Z then the 6, 7 and 8 rows forms a group. Again, if the pattern X X is repeated then 9th and 10th rows forms another group.
If I understand correctly you are trying to filter the rows of the csv file based on the values of the last column? Here is an example using nested for loops. 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)