Solving Attribute Error: ‘list’ object has no attribute ‘split’ in Python

Attribute Error: ‘list’ object has no attribute ‘split’ in Python is a common error which occurs when you try to map functionality over a frame of data or an array.

In this post we will try to solve your error and see why it occurs in the first place, we will present multiple solutions so you can find the one which suits your case.

Exploring the Attribute Error: ‘list’ object has no attribute ‘split’ in Python

This attribute error occurs when you try to map functionality over a frame of data or an array in Python.

The message of the attribute error looks very similar to this.

                                                                       #
'list' object has no attribute 'split'
                                                                       #

In the sections bellow we will explain the root of the error more and propose some possible fixes.

Solution : Convert readlines to string.

Since readlines is a list of strings.

The first solution is to convert readlines to string, you can do that by using str(). And more precisely readlines = str(readfile.readlines()) just like in the example bellow.

                                                                       #
`def getQuakeData():
filename = input("Enter the data: ")
readfile = open(filename, "r")
readlines = str(readfile.readlines())
Type = readlines.split(",")
a = Type[1]
b = Type[2]
for points in Type:
print(a,b)
getQuakeData()`
                                                                       #

Try the option above since it is the easiest and quickest solution to the error.

You can also use two libraries, one is called Pandas, the other is called NumPy in order to map functionality over a frame of data or an array.

The solution above should be enough to solve the problem, if you like my effort make sure to consider donating to our Kofi account, there is a red button that you can use if you are feeling generous.

Summing-up

This is the end of our article, I hope one of the above solutions was enough to solve the issue, make sure to keep learning and keep coding, Python is awesome and it is a skill worth learning, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/