Вы должны по крайней мере проверить свой список нерезкости.Это неправильно:
commentText = {row[2]}
if commentText in {row[2]}:
Это никогда не соответствует действительности, потому что вы проверяете:
if {"something"} in { "something" }:
, что False
, потому что .. его нет в нем:o)
Еще лучше было бы использовать набор и set.intersection () :
Создать невнятный файл:
with open('target_file.csv', "w", encoding="utf8") as f:
f.write("id,no idea,comment,likes, what columns,you,have\n")
f.write("1,,bla SlurZ bla,10,,,\n")
f.write("2,,bla SlurZ bla,20,,,\n")
f.write("3,,bla SlurZ. bla,30,,,\n")
f.write("4,,bla no bla,40,,,\n")
f.write("5,,bla no bla,50,,,\n")
f.write("6,,bla no bla,60,,,\n")
f.write("7,,bla no bla,70,,,\n")
f.write("8,,bla slurX- bla,80,,,\n")
f.write("9,,bla SlurZ bla,90,,,\n")
f.write("10,,bla SlurZ bla,100,,,\n")
f.write("11,,bla SlurZ bla,110,,,\n")
Программа:
import csv
slurCount = 0
noSlurCount = 0
line_count = 0
with open('target_file.csv', encoding="utf8") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
# use a set
slurs = {"slurX", "slurY", "SlurZ", "slurETC"}
# get the header
header = ", ".join(next(csv_reader))
print(f'Column names are {header}')
for row in csv_reader:
line_count += 1
# you need to clean the comment-words from punctuation marks
# so it detects slurY. or slurY- as slur as well
if slurs.intersection( (x.strip(",.-!?: ") for x in row[2].split() ) ):
slurCount += 1
print ("\t Comment contained a slur:")
print (f"\t\t{row[2]}")
else:
noSlurCount += 1
print ("\t Comment didn't contain a slur")
print(f'\t\t The comment ID is: {row[0]}')
print(f'\t\t Their comment was: {row[2]}')
print(f'\t\t The comment received: {row[3]} likes.')
print(f'Processed {line_count} lines.')
print(f'Found {slurCount} comments with slurs.')
print(f'Found {noSlurCount} comments without slurs.')
Выход:
Column names are id, no idea, comment, likes, what columns, you, have
Comment contained a slur:
bla SlurZ bla
Comment contained a slur:
bla SlurZ bla
Comment contained a slur:
bla SlurZ. bla
Comment didn't contain a slur
The comment ID is: 4
Their comment was: bla no bla
The comment received: 40 likes.
Comment didn't contain a slur
The comment ID is: 5
Their comment was: bla no bla
The comment received: 50 likes.
Comment didn't contain a slur
The comment ID is: 6
Their comment was: bla no bla
The comment received: 60 likes.
Comment didn't contain a slur
The comment ID is: 7
Their comment was: bla no bla
The comment received: 70 likes.
Comment contained a slur:
bla slurX- bla
Comment contained a slur:
bla SlurZ bla
Comment contained a slur:
bla SlurZ bla
Comment contained a slur:
bla SlurZ bla
Processed 11 lines.
Found 7 comments with slurs.
Found 4 comments without slurs.
Доку: