[英]syntax errors on creating wordDictionary of word and occurences
Having Attribute error issue on line 32. Requesting some assistance figuring out how to display word and occurrences.在第 32 行出现属性错误问题。请求一些帮助以弄清楚如何显示单词和出现。
import re
file_object = open('dialog.txt')
# read the file content
fileContents = file_object.read()
# convert fileContents to lowercase
final_dialog = fileContents.lower()
# print(final_dialog)
# replace a-z and spaces with cleanText variable
a_string = final_dialog
cleanText = re.sub("[^0-9a-zA-Z]+", "1", a_string)
# print(cleanText)
# wordlist that contains all words found in cleanText
text_string = cleanText
wordList = re.sub("1"," ", text_string)
# print(wordList)
#wordDictionary to count occurrence of each word to list in wordList
wordDictionary = dict()
#loop through .txt
for line in list(wordList):
# remove spaces and newline characters
line = line.strip()
# split the line into words
words = line.split()
#iterate over each word in line
for word in words.split():
if word not in wordDictionary:
wordDictionary[word] = 1
else:
wordDictionary[word] += 1
# print contents of dictionary
print(word)
# print file content
# print(fileContents)
# close file
# file_object.close()
Having Attribute error issue on line 32. Requesting some assistance figuring out how to display word and occurrences.在第 32 行出现属性错误问题。请求一些帮助以弄清楚如何显示单词和出现。
I think the error is我认为错误是
for word in words.split():
and should be replaced with并且应该替换为
for word in words:
Explanation: words
is already a list.解释:
words
已经是一个列表。 A list has no split
method, so you'll get an AttributeError
when trying to call that method.列表没有
split
方法,因此在尝试调用该方法时会收到AttributeError
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.