Возникли проблемы при поиске определенных строк внутри файла CSV - PullRequest
1 голос
/ 19 марта 2019

У меня есть комментарии на YouTube в виде файлов .csv, и я пытаюсь найти конкретные слова внутри комментариев.У меня есть список, с которым, я думаю, я сравниваю строку, содержащую комментарии, но, похоже, она не добавляется к slurCount, когда он встречает термин в этом списке, в то время как noSlurCount подсчитывает все комментарии.

import csv

slurCount = 0
noSlurCount = 0

with open('target_file.csv', encoding="utf8") as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    slurList = ["slurX", "slurY", "SlurZ", "slurETC"]
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            print(f'Column names are {", ".join(row)}')
            line_count += 1
        else:
            commentText = {row[2]}
            if commentText in {row[2]}:
                slurCount += 1
                print ("\t Comment contained a slur")
            else:
                noSlurCount += 1
                print ("\t Comment didn't contain a slur")
            print(f'\t The comment ID is: {row[0]}')
            print(f'\t Their comment was: {row[2]}')
            print(f'\t The comment received: {row[3]} likes.')
            line_count += 1
    print(f'Processed {line_count} lines.')
    print(f'Found {slurCount} comments with slurs.')
    print(f'Found {noSlurCount} comments without slurs.')`

Любая помощь будет потрясающей

1 Ответ

1 голос
/ 19 марта 2019

Вы должны по крайней мере проверить свой список нерезкости.Это неправильно:

           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.

Доку:

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...