dimanche 3 juin 2018

Python if statement wont execute for csv

I am trying to count the number of times a certain word appears in a csv file.

import csv

path = r'C:\Users\Ahmed Ismail Khalid\Desktop\test.csv'
str = "line"
count = 0


with open(path,'rt',encoding="utf-8") as f :
    reader = csv.reader(f)
    for row in reader :
       print(row)
       if str == row[0] :
       count = count + 1

print("\n \n The count is :",count)

Whenever i run the code, I always get the output 0 for count. However, all the rows are printed out. My csv file is has two columns, id and text and the data is as below :

id               text
1                this is line 1
2                this is line 2
3                this is line 3
4                this is line 4

You can see that all the lines contain str and the count should be 4 but it is always printed as 0.

Any help would be appreciated.

Thanks

Aucun commentaire:

Enregistrer un commentaire