'Adding unique values into csv , from reading csv file

I have csv file, I am reading data from it and writing it in other csv file. I am checking if the first 4 letters of particular line are "AGCS" then I will add that line in csv file and if the letters are different then I have to merge all the lines to single line and then add in csv file.

with open('New_Theme.csv', 'r') as inf, open("New_Theme1.csv", "w") as outf:
data = []
z1 = 0
reader = csv.reader(inf)
csv_rows = [row for row in reader]
# temp = ""
for row in csv_rows:
    # print(type(row))
    # print(row)
    if row[z1][0:4] == 'AGCS':
        data = row
        print(data)
        xy = " ".join(data)
        outf.write(xy + "\n")

    else:
        if row not in data:
            data = data + row
            print(data)
            xy = " ".join(data)
            outf.write(xy + "\n")

[input] Project Key,Jmp Issue,Summary,Updated,Created,Comments

AGCSPSR AGCSPSR-369 Onerous"

contract testing"

refinement"

AGCSPSR,AGCSPSR-380,Switc

Output[wrong] Project Key,Jmp Issue,Summary,Updated,Created,Comments

AGCSPSR AGCSPSR-369 Onerous"

AGCSPSR AGCSPSR-369 Onerous contract testing"

AGCSPSR AGCSPSR-369 Onerous contract testing refinement"

AGCSPSR,AGCSPSR-380,Switch

Output[Expected] Project Key,Jmp Issue,Summary,Updated,Created,Comments

AGCSPSR AGCSPSR-369 Onerous contract testing refinement"

AGCSPSR,AGCSPSR-380,Switch



Solution 1:[1]

Your for loop is adding sentences that have been added before.

You need to squeeze in a conditional statement that doesn't add previous added values. It can also be done by using a conditional statement that check whether or not the previous value is the same as the new one by checking a larger part of the data (e.g. checking up until row[z1][0:10]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sidereal