asfa afs
asfa afs

Reputation: 21

compare two fields in csv file

If I have

csv_reader = csv.DictReader(csv_file)
for line in csv_reader:
    if line['Title'] == ???:

and want to check if the field in each line (row) in column Title is the same as the field right after it in the next line. For extra clarification: if you consider row to be x and column to be y: I want to check if (row, column) == (row + 1, column) where column is called Title. How can I achieve this? I tried doing if line['Title'] == next(line)['Title']: but it doesn't work.

Upvotes: 0

Views: 91

Answers (1)

flaxon
flaxon

Reputation: 942

there is many ways to achieve it, but one of them is this

csv_reader = list(csv.DictReader(csv_file))
for i in range(len(csv_reader)-1):
    if csv_reader[i]['Title'] == csv_reader[i+1]['Title']:
        

Upvotes: 1

Related Questions