Issue Spliting String in Python


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue Spliting String in Python
# 1  
Old 05-18-2016
BSD Issue Spliting String in Python

I have a string like below
Quote:
jdbc_trgt=comp, new_com, com_one, comm
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.
Code:
    for word in jdbc_trgt.split(','):
        data_source.addTarget(getMBean('/Clusters/' + word))

How can we achive this in python?
# 2  
Old 05-18-2016
Code:
jdbc_trgt="comp, new_com, com_one, comm"

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

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 05-18-2016
Can i simply assign it to a variable called "comp" and use it elsewhere like the below?

Quote:
for word in jdbc_trgt.split(','):
comp=word.strip()
data_source.addTarget(getMBean('/Clusters/' + comp))
print comp
# 4  
Old 05-18-2016
Yes, you could, but remember Python has an strict indentation policy.

Code:
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.

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

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


Last edited by Aia; 05-18-2016 at 07:46 PM..
# 5  
Old 05-18-2016
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

[Python] - subprocess issue

So I have this basic script, see below import subprocess import shlex command = "gcloud projects list" subprocess.check_output(shlex.split(command)) subprocess.check_call(shlex.split(command)) The subprocess.check_call(shlex.split(command)) actually return what I expect. It returns... (6 Replies)
Discussion started by: scj2012
6 Replies

2. Programming

Python[Issue in converting .py to .exe using py2exe

Hi Experts, Good morning. I am trying to convert my hello.py to hello .exe file. I followed the steps as mentioned in the documentation but getting errors in the end. Please help. What I did as below-- Created hello.py file print ("Hello world!") raw_input('') Then... (0 Replies)
Discussion started by: shekhar_4_u
0 Replies

3. Shell Programming and Scripting

Python Thread Execution Issue . . .

Greetings! I set up a basic threading specimen which does the job:#!/usr/bin/python import threading class a(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): print("thread a finished") class b(threading.Thread): ... (0 Replies)
Discussion started by: LinQ
0 Replies

4. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

5. Shell Programming and Scripting

Spliting bash string into parts

Hello, let's say I have this string: string1="A\nB\nC D E\nFG\nH"; How can I split it so as to take every string separated with '\n' separately? For example, as for $string1, it would be split into string1_part1="A" string1_part2="B" string1_part3="C D E" string1_part4="FG"... (5 Replies)
Discussion started by: hakermania
5 Replies

6. Shell Programming and Scripting

Python String <--> Number

My question is so simple: A = raw_input("A ") if A == '56': VAR = (A + 54)/13 else: print "other operations" if I write in input 5656565656 i want to make some arithmetic operations if the first input is 56XXX but the output is TypeError: cannot concatenate 'str' and... (2 Replies)
Discussion started by: kazikamuntu
2 Replies

7. Shell Programming and Scripting

Crontab Permissions Issue with Python

I have a cron on a Linux server that isn't executing properly. CRON (with specific info replaced): MAILTO=emailaddress@server.com */2 * * * * python /data/site/cron.py OUTPUT: python: can't open file '/data/site/cron.py ': No such file or directoryAdditional info - The python path is... (3 Replies)
Discussion started by: theHire
3 Replies

8. Shell Programming and Scripting

awk help with string spliting

Hello all, I am having a problem with awk's string split function. I have a string that has a number at the end, I am trying to remove the alpha portion of the string and just have the numeric part. Here is my code and the result: BEGIN { word = "$category121"; split(word, a, 121) print... (2 Replies)
Discussion started by: RobertSubnet
2 Replies

9. Shell Programming and Scripting

Python - Scan for string

Hi i have a variable 'reform' and store the lines like reform= { record string(8) ID; string(4) PRD; date("YYMMDD", split = "800101") DateofManufact; string(4) PRDC_MODULE_NUM; string(1) END_OF_RECORD = "\n"; } I need to search for the character "\n"in the above variable... (1 Reply)
Discussion started by: dhanamurthy
1 Replies

10. Shell Programming and Scripting

With Regex Spliting the string into Alphanumeric and Numeric part

Hi there With shell script I'm trying to split the string into two parts. One is alphanumeric part, the other one is a numeric part. dummy_postcode_1 = 'SL1' --> res_alpha = 'SL' and res_numeric = '1' dummy_postcode_2 = 'S053' --> res_alpha = 'S' and res_numeric = '053' ... (1 Reply)
Discussion started by: ozgurgul
1 Replies
Login or Register to Ask a Question