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])

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("."))

another alternative:

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

Or DEVICE_IP.replace(".", "-")

oups... indeed :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.