when you call split() on a string it works fine not get anything error. And While are you trying to call the split() method on a list of strings, then you will face Attribute Error: ‘list’ object has no attribute ‘split’, this error.
In this article, we will discuss about this Attribute Error: ‘list’ object has no attribute ‘split’ in Python. And how to resolve the error all the possible solutions with examples.
How to Attribute Error: ‘list’ object has no attribute ‘split’ Occurs?
Most of the time you get this error while trying to call the split() method on a list of strings. It looks as like below:
"AttributeError: 'list' object has no attribute 'split'
How To Fix Attribute Error: ‘list’ object has no attribute ‘split’ Error?
There are different ways to fix Attribute Error: ‘list’ object has no attribute ‘split’ this error. Let us take a look at every solution.
- How To Fix Attribute Error: ‘list’ object has no attribute ‘split’ Error?
To Solve Attribute Error: ‘list’ object has no attribute ‘split’ Error to call split a list of strings, only a string. not call split on the whole list of lines. So, you need to split each line, not the whole thing. Now, Your error must be solved.
- Attribute Error: ‘list’ object has no attribute ‘split’ Error?
To Solve Attribute Error: ‘list’ object has no attribute ‘split’ Error to call split a list of strings, only a string. not call split on the whole list of lines. So, you need to split each line, not the whole thing. Now, Your error must be solved.
Solution 1: Use Splitting a List of Strings
def getQuakeData():
filename = input("Please enter the quake file: ")
readfile = open(filename, "r")
readlines = readfile.readlines()
Types = [line.split(",") for line in readlines]
xs = [Type[1] for Type in Types]
ys = [Type[2] for Type in Types]
for x, y in zip(xs, ys):
print(x,y)
getQuakeData()
Now, I hope your error must be solved.
Conclusion
In this article, we have discussed what causes the error and we have discussed ways to fix the error.
we hope this article has been informative. Thank you for reading. Kindly comment and let us know if you found it helpful.