Reputation: 803
I'm trying to figure out how to split data received from a socket. I have the sockets working and I can get the data correctly but I now want to split the data retrieved and obtain the last string. Here is what I'm trying(data is the data received from the socket)
split = data.split(' ')
print split
print split[-1]
But when I try it it wont work for some reason. Like lets say I have the string received as "test test1" I want to get test1.
Upvotes: 1
Views: 1904
Reputation: 32392
Just a note that it is bad practice to use the names of common library functions to name objects in your own code.
Sometimes it creates errors, but it always creates confusion.
Upvotes: 0
Reputation: 993153
The following works for me:
>>> data = "chdir /"
>>> a = data.split(" ")
>>> a
['chdir', '/']
>>> a[-1]
'/'
This appears to be what you're looking for. The reason your code doesn't work likely lies with the exact content of data
.
Note that using .split(" ")
is slightly different than .split()
when you have multiple spaces in the original string. See for example:
>>> "chdir /".split(" ")
['chdir', '/']
>>> "chdir /".split(" ")
['chdir', '', '/']
>>> "chdir /".split()
['chdir', '/']
Upvotes: 1