-3

I have a python function that returns the following:

result = myfunction()
result will be e.g. "192.168.200.123, 02/12/2013 13:59:42, 02/12/2013 13:59:42"

i.e. a string that contains 3 values separated by commas.

How do I split this string into 3 new variables??

baloo
  • 7,635
  • 4
  • 27
  • 35
user1646528
  • 437
  • 2
  • 7
  • 15
  • 4
    Should you be in control of that python function, it might be nice to just return the three values as a tuple rather than a string (`return (a, b, c)`) – akaIDIOT Feb 14 '13 at 12:30
  • Possible duplicate of http://stackoverflow.com/questions/9703512/python-split-string-into-multiple-string/9703580 – CoffeeRain Feb 14 '13 at 14:49

3 Answers3

2
>>> s = "192.168.200.123, 02/12/2013 13:59:42, 02/12/2013 13:59:42"
>>> n = [e.strip() for e in s.split(',')]
>>> print n
['192.168.200.123', '02/12/2013 13:59:42', '02/12/2013 13:59:42']

n is now a list with three elements. If you know your string will be split into exactly three variables and you want to name them, use this:

a, b, c = [e.strip() for e in s.split(',')]

The strip is used to remove unwanted spaces before/after strings.

Junuxx
  • 14,011
  • 5
  • 41
  • 71
eumiro
  • 207,213
  • 34
  • 299
  • 261
2

Use the split function:

my_string = #Contains ','
split_array = my_string.split(',')
Ketouem
  • 3,820
  • 1
  • 19
  • 29
0
result = myfunction()
result will be e.g. "192.168.200.123, 02/12/2013 13:59:42, 02/12/2013 13:59:42"

Two ways to solve this:

In myfunction(), return a list or a tuple: return (a, b, c) or return [a, b, c].

Or, you can use the s.split() function:

result = my_function()
results = result.split(',')

You can simplify this further like such:

result = my_function().split(',')
Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94