L.C L.C - 1 day ago 5
Python Question

Python Calling global dictionary in another method

I have created and populated a dict in my readfiles.py? and now I need to call this Dict for use in another method in a For loop to see if i can match words from another list called CategoryGA to words that are in the sentences Cleanse variable in populated dict:

my dict is populated with a chat between two people this is a segment of the output from the dict:

{0: ['hi'], 1: ['32 m fresno'], 2: ['u?'], 3: ['"33/f/ca', ' how r u?"'], 4: ['got a cam?']}

The key value for this dict is the linenum and the variable Cleanse is the chat.


import re
import os

Chatfile = 'ChatLogs/#######/Chat1.txt'
Lexfile = 'Lexicons/########.txt'
cleanChat = dict()

def ReadChat():

with open(Chatfile) as file_read:
chat_content = file_read.readlines()
for linenum, line in enumerate(chat_content):
Regex = re.sub('<.*?>[^\S\r\n]', '', line)
#cleanChat = Cleanse
#print(linenum, cleanChat)
Cleanse = Regex.rstrip("\n").split(",")

cleanChat[linenum] = Cleanse




from collections import Counter

from Categories.GainingAccess import GA
from Readfiles import *

CategoryGA = GA
Hits = []
cleansedLex = []

def SpeechActCounter():
for line in cleanChat.values():
for section in line:
if any(word in section for word in CategoryGA):
#if any(word in line for word in CategoryGA):
Word_Hit = False
for word in CategoryGA:
if line.find(word) != -1:
Word_Hit = True
print('%s appeared on Line %s' % (word))

count = Counter(Hits)
for key, value in count.items():
print(key, ':', value)

This is my error:

Traceback (most recent call last):
File "C:/Users/Lewis Collins/Desktop/Test/main.py", line 32, in <module>
File "C:/Users/Lewis Collins/Desktop/Test/main.py", line 22, in SpeechActCounter
if line.find(word) != -1:
AttributeError: 'list' object has no attribute 'find'

Process finished with exit code 1


['hi asl?']

I receive limited output and the for loop seems to be broken as it does not attempted to match words in if statement and the print statement at bottom does not get reached.

Contents of my CategoryGA:

import re

GA = ["face", "cam", "donkey"]


You are iterating over the dictionary keys here:

for line in cleanChat:

Your keys are integers (line numbers), not strings or lists, so the word in line test complains because the right-hand operand, line is not an iterable object (integers are not containers, they don't contain other objects, so testing for membership makes no sense).

If you wanted to loop over the values, do so explicitly:

for line in cleanChat.values():

That way line is set to one of the lists in the dictionary, each a result of the Regex.rstrip("\n").split(",") operation.