I have a funny problem.

I am trying to split a string.

DEVICE_IP = '164.178.7.30'
ip = DEVICE_IP.spilt('.')
print ip[0]+'-'+ip[1]+'-'+ip[2]+'-'+ip[3]

The above code throws the error

Traceback (most recent call last):
  File "/home/englotk/test", line 2, in <module>
    ip = str(DEVICE_IP).spilt('.')
AttributeError: 'str' object has no attribute 'spilt'

However this works fine if i enter the line in the above code individually at the python prompt. i get the above error if i have the above code in a file 'test.py' and say 'python test.py'

Please let me know why is this so..

Thanks.

ip = str(DEVICE_IP) .spilt('.') .
Spelling error.
ip = str(DEVICE_IP).split('.')

Alternative print line.

print '%s-%s-%s-%s' % (ip[0],ip[1],ip[2],ip[3])

Edited 6 Years Ago by snippsat: n/a

ip = str(DEVICE_IP) .spilt('.') .
Spelling error.
ip = str(DEVICE_IP).split('.')

Alternative print line.

print '%s-%s-%s-%s' % (ip[0],ip[1],ip[2],ip[3])

i feel like an idiot now. Thanks a lot!

another alternative:

"-".join(DEVICE_IP.split("."))

Edited 6 Years Ago by jice: n/a

This question has already been answered. Start a new discussion instead.