user3849475
user3849475

Reputation: 339

AttributeError: object has no attribute 'split'

There is an error,when I try to split

l =[u'this is friday', u'holiday begin']
split_l =l.split()
print(split_l)

The error is:

Traceback (most recent call last):
  File "C:\Users\spotify_track2.py", line 19, in <module>
    split_l =l.split()
AttributeError: 'list' object has no attribute 'split'

So I don't have idea to deal with this kind of error.

Upvotes: 3

Views: 78585

Answers (2)

Rayees Ahmad
Rayees Ahmad

Reputation: 11

as split() is a string function, we simply need to convert the list object into string and then apply split as follows:

`>>`I =[u'this is friday', u'holiday begin']
`>>`I=str(I)
`>>`split_I =I.split()
`>>`print(split_I)

Upvotes: 0

Bhargav Rao
Bhargav Rao

Reputation: 52071

Firstly, do not name your variable as list

Secondly list does not have the function split It is str which has it.

Check the documentation for str.split

Return a list of the words in the string, using sep as the delimiter string

(emphasis mine)

So you need to do

l =[u'this is friday', u'holiday begin']
split_list =[i.split() for i in l]
print(split_list)

Which would print

[[u'this', u'is', u'friday'], [u'holiday', u'begin']]

Post Comment edit

To get what you expected you can try

>>> l =[u'this is friday', u'holiday begin']
>>> " ".join(l).split(" ")
[u'this', u'is', u'friday', u'holiday', u'begin']

or as mentioned below

>>> [j for i in split_list for j in i]
[u'this', u'is', u'friday', u'holiday', u'begin']

Upvotes: 10

Related Questions