. Advertisement .
..3..
. Advertisement .
..4..
I am tired of fixing the problem: attributeerror: ‘list’ object has no attribute ‘lower’ in the python; even if I get the reference from another forum, it still returns an error:
AttributeErrorTraceback (most recent call last)
<ipython-input-84-33bbe380449e> in <module>()
1 data = [line.strip() for line in open("C:\corpus\TermList.txt", 'r')]
----> 2 texts = [[word for word in data.lower().split()] for word in data]
3
AttributeError: 'list' object has no attribute 'lower'
To identify the problem, I will show you the detail here:
data = [line.strip() for line in open("C:\corpus\TermList.txt", 'r')]
texts = [[word for word in data.lower().split()] for word in data]
How do I do that? Could you support me in improving this problem?
The cause: You attempted to use
lower()
on a list of data butlower()
only works on strings.Solution:
Try this to fix the problem: attributeerror: ‘list’ object has no attribute ‘lower’:
You will need
This code will generate a list with lowercase words (
[word.lower() for word in line.split()]
) for everyline
(data
). Each strline
will have a sequence of space separated words.line.split()
will convert this sequence into a list.word.lower()
will change each word to lowercase.Python lists cannot be divided into separate lists based on characters that appear in the values of a list. This is unlike strings which values can be separated into a list. The AttributeError is an exception thrown when an object does not have the attribute you tried to access. The ‘list’ object has no attribute ‘split’ and you’re trying to call python split() function on the whole list of lines, and you can’t split a list of strings, only a string. So, you need to split each line, not the whole thing.
To solve the above problem, you need to iterate over the strings in the list to get individual strings; then, you can call the split() function.