我在readfiles.py中创建并填充了一个字典?现在我需要调用这个Dict用于For循环中的另一个方法,以查看我是否可以将另一个名为CategoryGA的列表中的单词与填充的dict中的句子Cleanse变量中的单词匹配:
我的dict填充了两个人之间的聊天,这是dict输出的一部分:
{0: ['hi'], 1: ['32 m fresno'], 2: ['u?'], 3: ['"33/f/ca', ' how r u?"'], 4: ['got a cam?']}
此dict的关键值是linenum,变量Cleanse是聊天。
readfiles.py
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
file_read.close()
ReadChat()
main.py
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):
print(section)
#if any(word in line for word in CategoryGA):
print(line)
Word_Hit = False
for word in CategoryGA:
if line.find(word) != -1:
Word_Hit = True
Hits.append(word)
print('%s appeared on Line %s' % (word))
count = Counter(Hits)
count.keys()
for key, value in count.items():
print(key, ':', value)
SpeechActCounter()
这是我的错误:
Traceback (most recent call last):
File "C:/Users/Lewis Collins/Desktop/Test/main.py", line 32, in <module>
SpeechActCounter()
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?']
我收到有限的输出,并且for循环似乎被打破了,因为它没有尝试匹配if语句中的单词,并且底部的print语句无法到达。
我的CategoryGA的内容:
import re
GA = ["face", "cam", "donkey"]
答案 0 :(得分:0)
您正在迭代词典 keys :
for line in cleanChat:
您的键是整数(行数字),而不是字符串或列表,因此word in line
测试会抱怨,因为右侧操作数line
不是可迭代对象(整数不是容器,它们不包含其他对象,因此对成员资格的测试毫无意义。)
如果您想循环值,请明确地执行此操作:
for line in cleanChat.values():
这样line
设置为字典中的一个列表,每个列表都是Regex.rstrip("\n").split(",")
操作的结果。