Search target file for terms saved in a .txt or .csv and re.findall?

relatively new to python and coding, but I think I’ve slammed my head against the wall enough for this one. I’ve come to stack overflow for sage advice.

I am writing a program that will search through a target folder for predefined terms stored in a “keyterms.csv.” keyterms.csv has one column of words and phrases separated by a newline, nothing else. When a word or phrase from the keyterms file is found in a file in the target directory, the program returns the name of the culprit file(s) with number of entries of the keyterm found.

Traceback (most recent call last):
count = len(re.findall(self.ktxt, ttxt))
TypeError: unhashable type: 'dict'

so I think the answer is obvious that re.findall only works with strings or something that spits out only strings, and will not work with dicts, lists or anything else like that. I have tried several snippets of code from SO to split the read file into individual strings to feed into re.findall, to no avail. Should I be feeding the strings into an array or something and for.each the array? not sure what exactly I’m missing.

disregard my copious print statements, just using them to trace what’s going on.

def read_key and def find are where the problem lies as far as I can tell. I have tried splitting self.ktxt = list(reader) intro strings and sending it to def find in several ways but maybe I just fundamentally don’t understand what’s going on with it.

import os
import re
import csv


class FindKterms:

    def __init__(self, path, query):
        self.path = path
        self.query = query
        self.ktxt = {}
        self.searched = {}
        print('def init complete ...')

    def read_key(self):
        print('opening keyterms.csv')
        with open(self.query, newline='', encoding='utf_8') as f:
            reader = csv.reader(f, delimiter='n')
            self.ktxt = list(reader)

    def find(self):
        if self.path[-1] != '/':
            self.path += '/'
            print('Appended "/" to given filepath')
        print('starting find function ...')
        for root, dirs, files in os.walk(self.path):
            for file in files:
                print('Searching: ' + file + ' ...')
                f = open(root + file)
                ttxt = f.read()
                f.close()
                print('successfully read and closed ' + file)

                count = len(re.findall(self.ktxt, ttxt))
                if count > 0:
                    self.searched[root + file] = count

    def get_results(self):
        return self.searched

the main() seems to work fine calling the functions and moving data around.is calling the functions and seems to work fine. Just the strings from keyterms.csv need to be used as a match case for re.findall across the target directory.

thanks for any help, I feel like I’m right there but the solution eludes me.

New contributor

3xogenic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật