0

I have below python script:

conn=boto.ec2.connect_to_region("us-west-1")
result=collections.defaultdict(list)
reservations = conn.get_all_instances()
for res in reservations:
        for inst in res.instances:
                if 'clusters' in inst.tags:
                        values = inst.tags['clusters']
                        print values

and the output is:

baka-alpha,baka-beta
app
app-demo,dhivart
api
sahar-du,app,api

I want to store each value in a list as:

['baka-alpha', 'baka-beta', 'app', 'app-demo', 'dhivart', 'api', 'sahar-du', 'app', 'api'] 

How can I do so? Please let me know.

SJJ
  • 53
  • 1
  • 1
  • 5
  • It didn't add any characters. Those characters just mean that the text is in unicode. Here is the [a related post](http://stackoverflow.com/questions/2464959/whats-the-u-prefix-in-a-python-string). – Moon Cheesez Jun 05 '16 at 07:32
  • oh, nice to know. Thanks – SJJ Jun 05 '16 at 10:50

1 Answers1

2

If the string that you want to split is already present in a variable, like values, you could try the following:

vl_list = [] # create a list for values
for line in values.split("\n"):
    vl_list.extend(line.split(","))

After this is complete, you'll have your values in a list vl_list

If the string is being read from stdin and you don't know, how many lines will be provided, you could do this instead:

from sys import stdin
vl_list = []
for line in stdin.readlines():
    vl_list.extend(line.split(","))

EDIT:
Your variable values does not contain all the values after the loop. So you can insert the splitting part straight into the loop and you'll get everything

vl_list = []
conn=boto.ec2.connect_to_region("us-west-1")
result=collections.defaultdict(list)
reservations = conn.get_all_instances()
for res in reservations:
        for inst in res.instances:
                if 'clusters' in inst.tags:
                        values = inst.tags['clusters']
                        vl_list.extend(values.split(","))
                        print values
illright
  • 3,991
  • 2
  • 29
  • 54
  • I am getting multiple None as output – SJJ Jun 05 '16 at 08:13
  • above output is stored in a variable named values. I need to store the values in list as [baka-alpha, baka-beta, app, app-demo, dhivart, api, sahar-du, app, api] – SJJ Jun 05 '16 at 08:25
  • @SJJ What is the type of your variable `values`? You can check it by printing `type(values)`. Make sure it is a string. Also, try printing `repr(values)` and check if you get this printed: `'baka-alpha,baka-beta\napp\napp-demo,dhivart\napi\nsahar-du,app,api'`. This is how it should be stored. Because the above code works for me. [Here](http://ideone.com/OGuEVA) is the link to the online compiler that shows the code in action – illright Jun 05 '16 at 09:30
  • printing `type(values)` returns **** & `repr(values)` returns like **u'baka-alpha,baka-beta'**, then new line, then **u'app'** – SJJ Jun 05 '16 at 10:33
  • @SJJ That is weird. Could you edit the question to show exactly how do you get the content of `values`? – illright Jun 05 '16 at 10:36
  • @SJJ Take a look at the answer – illright Jun 05 '16 at 10:51
  • Great! It worked liked a charm. Thank you so much! I just had to modify `values = str(inst.tags['clusters'])` and bring `print vl_list` out of loop. That's all! – SJJ Jun 05 '16 at 11:03