0

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

2
  • There appaers to be an intendation error: count = count + 1 Commented Jun 3, 2018 at 12:27
  • I think it should be 'if str == row[1]' Commented Jun 3, 2018 at 12:31

3 Answers 3

1

Specify 'newline' , 'delimiter' and 'quote char' exactly to get your desired result when opening csv file. See the example below:

>>> import csv
>>> with open('eggs.csv', newline='') as csvfile:
...     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
...     for row in spamreader:
...         print(', '.join(row))
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam
Sign up to request clarification or add additional context in comments.

Comments

0
import csv

count = 0
find = 'line'

with open('test.csv', 'rt', encoding='utf-8') as f:
    reader = csv.reader(f)
    for row in reader:
        line = ', '.join(row)
        if find in line:
            count = count + 1

print('The count is :', count)

FYI: You can avoid join if you want to calculate how many times word appears in the file. Just read file by lines without the csv module.

1 Comment

Thanks this worked. But what do you mean by reading without csv module? Treat the file as a text file?
0

Use in to look for your string.

Ex:

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

Edit as per comment

import csv
checkStr = 'line'
result = []
with open(path,'rt',encoding="utf-8") as f :
    reader = csv.reader(f)
    for row in reader :
       if checkStr not in row[1]:    #Check if 'line' not in row
           result.append(row)

with open(filename,'w') as f:        #Write new result. 
    writer = csv.writer(f)
    for row in result:
        writer.writerow(row)
  • Append all rows that do not have checkStr
  • Write the result to new file.

5 Comments

Tried that but the error still persists. Still getting count to be 0
Updated snippet use row[1]
Please accept one of the ans that helped you. Thanks
Any idea how i could delete the entire row if it contains str? That would be helpful and would certainly save me from starting another thread
Updated snippet. Please check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.