Issue Spliting String in Python

I have a string like below

Note: I have have a single to any number of comma "," seperated string assigned to jdbc_trgt variable.

I need to split jdbc_trgt using comma(,) as the delimiter.

I tried the below but it fails as i dont know how can i read each split string iterately.

    for word in jdbc_trgt.split(','):
        data_source.addTarget(getMBean('/Clusters/' + word))

How can we achive this in python?

jdbc_trgt="comp, new_com, com_one, comm"

for word in jdbc_trgt.split(','):
   data_source.addTarget(getMBean('/Clusters/' + word.strip()))
1 Like

Can i simply assign it to a variable called "comp" and use it elsewhere like the below?

Yes, you could, but remember Python has an strict indentation policy.

for word in jdbc_trgt.split(','):
    comp=word.strip()
    data_source.addTarget(getMBean('/Clusters/' + comp))
    print comp 

...And comp will not be comp all the time, but it could be new_com or com_one or comm

You could make an array, outside the loop.

jdbc_trgt= ['comp', 'new_com', 'com_one', 'comm']

for word in jdbc_trgt:
    data_source.addTarget(getMBean('/Clusters/' + word))
jdbc_trgt="comp, new_com, com_one, comm"

for word in jdbc_trgt.split(','):
   global comp
   comp=word.strip();
   data_source.addTarget(getMBean('/Clusters/' + word.strip()))
   print comp

print comp